Skip to content

Commit

Permalink
Rewrite tests in storage_test.go to table-driven tests
Browse files Browse the repository at this point in the history
Signed-off-by: Jacek Ewertowski <[email protected]>
  • Loading branch information
jewertow committed Dec 29, 2021
1 parent 8deab18 commit 9c06e0a
Showing 1 changed file with 159 additions and 146 deletions.
305 changes: 159 additions & 146 deletions pkg/apis/k0s.k0sproject.io/v1beta1/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,189 +117,202 @@ type storageSuite struct {
}

func (s *storageSuite) TestValidation() {
s.T().Run("default_storage_spec_is_valid", func(t *testing.T) {
spec := DefaultStorageSpec(constant.DataDirDefault)

s.Nil(spec.Validate())
})

s.T().Run("internal_cluster_spec_is_valid", func(t *testing.T) {
spec := &StorageSpec{
Type: EtcdStorageType,
Etcd: &EtcdConfig{
PeerAddress: "192.168.10.10",
var validStorageSpecs = []struct {
desc string
spec *StorageSpec
}{
{
desc: "default_storage_spec_is_valid",
spec: DefaultStorageSpec(constant.DataDirDefault),
},
{
desc: "internal_cluster_spec_is_valid",
spec: &StorageSpec{
Type: EtcdStorageType,
Etcd: &EtcdConfig{
PeerAddress: "192.168.10.10",
},
},
}

s.Nil(spec.Validate())
})

s.T().Run("external_cluster_spec_without_tls_is_valid", func(t *testing.T) {
spec := &StorageSpec{
Type: EtcdStorageType,
Etcd: &EtcdConfig{
ExternalCluster: &ExternalCluster{
Endpoints: []string{"http://192.168.10.10"},
EtcdPrefix: "tenant-1",
},
{
desc: "external_cluster_spec_without_tls_is_valid",
spec: &StorageSpec{
Type: EtcdStorageType,
Etcd: &EtcdConfig{
ExternalCluster: &ExternalCluster{
Endpoints: []string{"http://192.168.10.10"},
EtcdPrefix: "tenant-1",
},
},
},
}

s.Nil(spec.Validate())
})

s.T().Run("external_cluster_spec_with_tls_is_valid", func(t *testing.T) {
spec := &StorageSpec{
Type: EtcdStorageType,
Etcd: &EtcdConfig{
ExternalCluster: &ExternalCluster{
Endpoints: []string{"http://192.168.10.10"},
EtcdPrefix: "tenant-1",
CaFile: "/etc/pki/CA/ca.crt",
ClientCertFile: "/etc/pki/tls/certs/etcd-client.crt",
ClientKeyFile: "/etc/pki/tls/private/etcd-client.key",
},
{
desc: "external_cluster_spec_with_tls_is_valid",
spec: &StorageSpec{
Type: EtcdStorageType,
Etcd: &EtcdConfig{
ExternalCluster: &ExternalCluster{
Endpoints: []string{"http://192.168.10.10"},
EtcdPrefix: "tenant-1",
CaFile: "/etc/pki/CA/ca.crt",
ClientCertFile: "/etc/pki/tls/certs/etcd-client.crt",
ClientKeyFile: "/etc/pki/tls/private/etcd-client.key",
},
},
},
}
},
}

s.Nil(spec.Validate())
})
for _, tt := range validStorageSpecs {
s.T().Run(tt.desc, func(t *testing.T) {
s.Nil(tt.spec.Validate())
})
}

s.T().Run("external_cluster_endpoints_and_etcd_prefix_cannot_be_empty", func(t *testing.T) {
spec := &StorageSpec{
Type: EtcdStorageType,
Etcd: &EtcdConfig{
ExternalCluster: &ExternalCluster{
Endpoints: []string{},
EtcdPrefix: "",
var singleValidationErrorCases = []struct {
desc string
spec *StorageSpec
expectedErrMsg string
}{
{
desc: "external_cluster_endpoints_cannot_be_null",
spec: &StorageSpec{
Type: EtcdStorageType,
Etcd: &EtcdConfig{
ExternalCluster: &ExternalCluster{
Endpoints: nil,
EtcdPrefix: "tenant-1",
},
},
},
}

errs := spec.Validate()
s.NotNil(errs)
s.Len(errs, 2)
s.Contains(errs[0].Error(), "spec.storage.etcd.externalCluster.endpoints cannot be null or empty")
s.Contains(errs[1].Error(), "spec.storage.etcd.externalCluster.etcdPrefix cannot be empty")
})

s.T().Run("external_cluster_endpoints_cannot_be_null", func(t *testing.T) {
spec := &StorageSpec{
Type: EtcdStorageType,
Etcd: &EtcdConfig{
ExternalCluster: &ExternalCluster{
Endpoints: nil,
EtcdPrefix: "tenant-1",
expectedErrMsg: "spec.storage.etcd.externalCluster.endpoints cannot be null or empty",
},
{
desc: "external_cluster_endpoints_cannot_contain_empty_strings",
spec: &StorageSpec{
Type: EtcdStorageType,
Etcd: &EtcdConfig{
ExternalCluster: &ExternalCluster{
Endpoints: []string{"http://192.168.10.2:2379", ""},
EtcdPrefix: "tenant-1",
},
},
},
}

errs := spec.Validate()
s.NotNil(errs)
s.Len(errs, 1)
s.Contains(errs[0].Error(), "spec.storage.etcd.externalCluster.endpoints cannot be null or empty")
})

s.T().Run("external_cluster_endpoints_cannot_contain_empty_strings", func(t *testing.T) {
spec := &StorageSpec{
Type: EtcdStorageType,
Etcd: &EtcdConfig{
ExternalCluster: &ExternalCluster{
Endpoints: []string{"http://192.168.10.2:2379", ""},
EtcdPrefix: "tenant-1",
expectedErrMsg: "spec.storage.etcd.externalCluster.endpoints cannot contain empty strings",
},
{
desc: "external_cluster_must_have_configured_all_tls_properties_or_none_of_them",
spec: &StorageSpec{
Type: EtcdStorageType,
Etcd: &EtcdConfig{
ExternalCluster: &ExternalCluster{
Endpoints: []string{"http://192.168.10.10"},
EtcdPrefix: "tenant-1",
CaFile: "",
ClientCertFile: "/etc/pki/tls/certs/etcd-client.crt",
ClientKeyFile: "",
},
},
},
}
expectedErrMsg: "spec.storage.etcd.externalCluster is invalid: all TLS properties [caFile,clientCertFile,clientKeyFile] must be defined or none of those",
},
}

errs := spec.Validate()
s.NotNil(errs)
s.Len(errs, 1)
s.Contains(errs[0].Error(), "spec.storage.etcd.externalCluster.endpoints cannot contain empty strings")
})
for _, tt := range singleValidationErrorCases {
s.T().Run(tt.desc, func(t *testing.T) {
errs := tt.spec.Validate()
s.NotNil(errs)
s.Len(errs, 1)
s.Contains(errs[0].Error(), tt.expectedErrMsg)
})
}

s.T().Run("external_cluster_must_have_configured_all_tls_properties_or_none_of_them", func(t *testing.T) {
s.T().Run("external_cluster_endpoints_and_etcd_prefix_cannot_be_empty", func(t *testing.T) {
spec := &StorageSpec{
Type: EtcdStorageType,
Etcd: &EtcdConfig{
ExternalCluster: &ExternalCluster{
Endpoints: []string{"http://192.168.10.10"},
EtcdPrefix: "tenant-1",
CaFile: "",
ClientCertFile: "/etc/pki/tls/certs/etcd-client.crt",
ClientKeyFile: "",
Endpoints: []string{},
EtcdPrefix: "",
},
},
}

errs := spec.Validate()
s.NotNil(errs)
s.Len(errs, 1)
s.Contains(errs[0].Error(), "spec.storage.etcd.externalCluster is invalid: "+
"all TLS properties [caFile,clientCertFile,clientKeyFile] must be defined or none of those")
s.Len(errs, 2)
s.Contains(errs[0].Error(), "spec.storage.etcd.externalCluster.endpoints cannot be null or empty")
s.Contains(errs[1].Error(), "spec.storage.etcd.externalCluster.etcdPrefix cannot be empty")
})
}

func (s *storageSuite) TestIsTLSEnabled() {
s.T().Run("is_TLS_enabled_returns_true_when_internal_cluster_is_used", func(t *testing.T) {
spec := &StorageSpec{
Type: EtcdStorageType,
Etcd: &EtcdConfig{
PeerAddress: "192.168.10.10",
var storageSpecs = []struct {
desc string
spec *StorageSpec
expectedResult bool
}{
{
desc: "is_TLS_enabled_returns_true_when_internal_cluster_is_used",
spec: &StorageSpec{
Type: EtcdStorageType,
Etcd: &EtcdConfig{
PeerAddress: "192.168.10.10",
},
},
}

result := spec.Etcd.IsTLSEnabled()
s.True(result)
})

s.T().Run("is_TLS_enabled_returns_true_when_external_cluster_is_used_and_has_set_all_TLS_properties", func(t *testing.T) {
spec := &StorageSpec{
Type: EtcdStorageType,
Etcd: &EtcdConfig{
ExternalCluster: &ExternalCluster{
Endpoints: []string{"http://192.168.10.10"},
EtcdPrefix: "tenant-1",
CaFile: "/etc/pki/CA/ca.crt",
ClientCertFile: "/etc/pki/tls/certs/etcd-client.crt",
ClientKeyFile: "/etc/pki/tls/private/etcd-client.key",
expectedResult: true,
},
{
desc: "is_TLS_enabled_returns_true_when_external_cluster_is_used_and_has_set_all_TLS_properties",
spec: &StorageSpec{
Type: EtcdStorageType,
Etcd: &EtcdConfig{
ExternalCluster: &ExternalCluster{
Endpoints: []string{"http://192.168.10.10"},
EtcdPrefix: "tenant-1",
CaFile: "/etc/pki/CA/ca.crt",
ClientCertFile: "/etc/pki/tls/certs/etcd-client.crt",
ClientKeyFile: "/etc/pki/tls/private/etcd-client.key",
},
},
},
}

result := spec.Etcd.IsTLSEnabled()
s.True(result)
})

s.T().Run("is_TLS_enabled_returns_false_when_external_cluster_is_used_but_has_no_TLS_properties", func(t *testing.T) {
spec := &StorageSpec{
Type: EtcdStorageType,
Etcd: &EtcdConfig{
ExternalCluster: &ExternalCluster{
Endpoints: []string{"http://192.168.10.10"},
EtcdPrefix: "tenant-1",
expectedResult: true,
},
{
desc: "is_TLS_enabled_returns_false_when_external_cluster_is_used_but_has_no_TLS_properties",
spec: &StorageSpec{
Type: EtcdStorageType,
Etcd: &EtcdConfig{
ExternalCluster: &ExternalCluster{
Endpoints: []string{"http://192.168.10.10"},
EtcdPrefix: "tenant-1",
},
},
},
}

result := spec.Etcd.IsTLSEnabled()
s.False(result)
})

s.T().Run("is_TLS_enabled_returns_false_when_external_cluster_is_used_but_TLS_properties_are_configured_partially", func(t *testing.T) {
spec := &StorageSpec{
Type: EtcdStorageType,
Etcd: &EtcdConfig{
ExternalCluster: &ExternalCluster{
Endpoints: []string{"http://192.168.10.10"},
EtcdPrefix: "tenant-1",
CaFile: "/etc/pki/CA/ca.crt",
expectedResult: false,
},
{
desc: "is_TLS_enabled_returns_false_when_external_cluster_is_used_but_TLS_properties_are_configured_partially",
spec: &StorageSpec{
Type: EtcdStorageType,
Etcd: &EtcdConfig{
ExternalCluster: &ExternalCluster{
Endpoints: []string{"http://192.168.10.10"},
EtcdPrefix: "tenant-1",
CaFile: "/etc/pki/CA/ca.crt",
},
},
},
}
expectedResult: false,
},
}

result := spec.Etcd.IsTLSEnabled()
s.False(result)
})
for _, tt := range storageSpecs {
s.T().Run(tt.desc, func(t *testing.T) {
result := tt.spec.Etcd.IsTLSEnabled()
s.Equal(result, tt.expectedResult)
})
}
}

func TestStorageSuite(t *testing.T) {
Expand Down

0 comments on commit 9c06e0a

Please sign in to comment.