Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: 🌱 added tests for loadTLSConfigFromEnv in factory_test.go #1589

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 71 additions & 28 deletions pkg/provisioner/ironic/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,35 +181,78 @@ func TestLoadConfigFromEnv(t *testing.T) {
}
}

func TestLoadEndpointsFromEnv(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do I understand correctly that you removed this test? Why?
I think we should keep it and add a separate test for the TLS config.

cases := []struct {
name string
env EnvFixture
expectError bool
}{
{
name: "with-ironic",
env: EnvFixture{
ironicEndpoint: "http://ironic.test",
},
}, {
name: "without-ironic",
env: EnvFixture{},
expectError: true,
},
func TestLoadTLSConfigFromEnv(t *testing.T) {
const (
TLSKeyFilePath = "/opt/metal3/certs/client/tls.key"
TLSCertFilePath = "/opt/metal3/certs/client/tls.crt"
)

os.Setenv("IRONIC_CACERT_FILE", "/path/to/ca.crt")
os.Setenv("IRONIC_CLIENT_CERT_FILE", "/path/to/client.crt")
os.Setenv("IRONIC_CLIENT_PRIVATE_KEY_FILE", "/path/to/client.key")
os.Setenv("IRONIC_INSECURE", "false")
os.Setenv("IRONIC_SKIP_CLIENT_SAN_VERIFY", "true")
defer func() {
os.Unsetenv("IRONIC_CACERT_FILE")
os.Unsetenv("IRONIC_CLIENT_CERT_FILE")
os.Unsetenv("IRONIC_CLIENT_PRIVATE_KEY_FILE")
os.Unsetenv("IRONIC_INSECURE")
os.Unsetenv("IRONIC_SKIP_CLIENT_SAN_VERIFY")
}()
Comment on lines +221 to +232
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you check the other tests in this file, you will see that they make use of a struct EnvFixture. There are helpful functions for setting and unsetting the environment from this struct. Put all of this together and you get a nice way of looping through test cases described by structs. Could you try to mimic this?


tlsConfig := loadTLSConfigFromEnv()

if tlsConfig.TrustedCAFile != "/path/to/ca.crt" ||
tlsConfig.ClientCertificateFile != "/path/to/client.crt" ||
tlsConfig.ClientPrivateKeyFile != "/path/to/client.key" ||
tlsConfig.InsecureSkipVerify != false ||
tlsConfig.SkipClientSANVerify != true {
t.Errorf("Unexpected TLS config values")
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
defer tc.env.TearDown()
tc.env.SetUp()
i, err := loadEndpointsFromEnv()
if tc.expectError {
assert.NotNil(t, err)
} else {
assert.Nil(t, err)
tc.env.VerifyEndpoints(t, i)
}
})
os.Setenv("IRONIC_CACERT_FILE", "/path/to/ca.crt")
os.Unsetenv("IRONIC_CLIENT_CERT_FILE")
os.Unsetenv("IRONIC_CLIENT_PRIVATE_KEY_FILE")
os.Unsetenv("IRONIC_INSECURE")
os.Unsetenv("IRONIC_SKIP_CLIENT_SAN_VERIFY")
defer func() {
os.Unsetenv("IRONIC_CACERT_FILE")
os.Unsetenv("IRONIC_CLIENT_CERT_FILE")
os.Unsetenv("IRONIC_CLIENT_PRIVATE_KEY_FILE")
os.Unsetenv("IRONIC_INSECURE")
os.Unsetenv("IRONIC_SKIP_CLIENT_SAN_VERIFY")
}()

tlsConfig = loadTLSConfigFromEnv()

if tlsConfig.TrustedCAFile != "/path/to/ca.crt" ||
tlsConfig.ClientCertificateFile != TLSCertFilePath ||
tlsConfig.ClientPrivateKeyFile != TLSKeyFilePath ||
tlsConfig.InsecureSkipVerify != false ||
tlsConfig.SkipClientSANVerify != false {
t.Errorf("Unexpected TLS config values")
}

os.Setenv("IRONIC_CACERT_FILE", "")
os.Setenv("IRONIC_CLIENT_CERT_FILE", "")
os.Setenv("IRONIC_CLIENT_PRIVATE_KEY_FILE", "")
os.Setenv("IRONIC_INSECURE", "")
os.Setenv("IRONIC_SKIP_CLIENT_SAN_VERIFY", "")
defer func() {
os.Unsetenv("IRONIC_CACERT_FILE")
os.Unsetenv("IRONIC_CLIENT_CERT_FILE")
os.Unsetenv("IRONIC_CLIENT_PRIVATE_KEY_FILE")
os.Unsetenv("IRONIC_INSECURE")
os.Unsetenv("IRONIC_SKIP_CLIENT_SAN_VERIFY")
}()

tlsConfig = loadTLSConfigFromEnv()

if tlsConfig.TrustedCAFile != "/opt/metal3/certs/ca/tls.crt" ||
tlsConfig.ClientCertificateFile != TLSCertFilePath ||
tlsConfig.ClientPrivateKeyFile != TLSKeyFilePath ||
tlsConfig.InsecureSkipVerify != false ||
tlsConfig.SkipClientSANVerify != false {
t.Errorf("Unexpected TLS config values")
}
}