@@ -18,47 +18,52 @@ package resources
1818
1919import (
2020 "context"
21+ "crypto/sha256"
22+ "fmt"
2123 "strings"
2224 "testing"
2325
26+ "github.com/martinlindhe/base36"
2427 corev1 "k8s.io/api/core/v1"
2528 "k8s.io/apimachinery/pkg/runtime"
2629 "sigs.k8s.io/controller-runtime/pkg/client"
2730 "sigs.k8s.io/controller-runtime/pkg/client/fake"
2831)
2932
33+ func expectedIdentityHash (identity string ) string {
34+ hash := sha256 .Sum224 ([]byte (identity ))
35+ return strings .ToLower (base36 .EncodeBytes (hash [:8 ]))
36+ }
37+
3038func TestCreateNamespace (t * testing.T ) {
3139 scheme := runtime .NewScheme ()
3240 _ = corev1 .AddToScheme (scheme )
3341
3442 tests := []struct {
35- name string
36- generateName string
37- identity string
38- author string
39- wantErr bool
40- wantNameGenerated bool
41- wantAnnotations map [string ]string
43+ name string
44+ generateName string
45+ identity string
46+ author string
47+ wantErr bool
48+ wantAnnotations map [string ]string
4249 }{
4350 {
44- name : "create new namespace with generateName" ,
45- generateName : "test-ns" ,
46- identity : "test-id-123" ,
47- author : "bob" ,
48- wantErr : false ,
49- wantNameGenerated : true ,
51+ name : "create new namespace with generateName" ,
52+ generateName : "test-ns" ,
53+ identity : "test-id-123" ,
54+ author : "bob" ,
55+ wantErr : false ,
5056 wantAnnotations : map [string ]string {
5157 IdentityAnnotationKey : "test-id-123" ,
5258 AuthorAnnotationKey : "bob" ,
5359 },
5460 },
5561 {
56- name : "create new namespace with generateName already ending with dash" ,
57- generateName : "test-ns-" ,
58- identity : "test-id-456" ,
59- author : "alice" ,
60- wantErr : false ,
61- wantNameGenerated : true ,
62+ name : "create new namespace with generateName already ending with dash" ,
63+ generateName : "test-ns-" ,
64+ identity : "test-id-456" ,
65+ author : "alice" ,
66+ wantErr : false ,
6267 wantAnnotations : map [string ]string {
6368 IdentityAnnotationKey : "test-id-456" ,
6469 AuthorAnnotationKey : "alice" ,
@@ -91,18 +96,16 @@ func TestCreateNamespace(t *testing.T) {
9196 return
9297 }
9398
94- expectedGenerateNamePrefix := tt .generateName
95- if ! strings .HasSuffix (expectedGenerateNamePrefix , "-" ) {
96- expectedGenerateNamePrefix += "-"
99+ expectedPrefix := tt .generateName
100+ expectedHash := expectedIdentityHash (tt .identity )
101+ expectedName := fmt .Sprintf ("%s-%s" , expectedPrefix , expectedHash )
102+
103+ if result .Name != expectedName {
104+ t .Errorf ("CreateNamespace() name = %v, expected %v" , result .Name , expectedName )
97105 }
98106
99- if tt .wantNameGenerated {
100- if ! strings .HasPrefix (result .Name , expectedGenerateNamePrefix ) {
101- t .Errorf ("CreateNamespace() name = %v, expected to start with %v" , result .Name , expectedGenerateNamePrefix )
102- }
103- if result .GenerateName != expectedGenerateNamePrefix {
104- t .Errorf ("CreateNamespace() generateName = %v, expected %v" , result .GenerateName , expectedGenerateNamePrefix )
105- }
107+ if result .GenerateName != "" {
108+ t .Errorf ("CreateNamespace() generateName = %v, expected empty string" , result .GenerateName )
106109 }
107110
108111 for key , expectedValue := range tt .wantAnnotations {
0 commit comments