github.com/grafana/tanka@v0.26.1-0.20240506093700-c22cfc35c21a/pkg/process/process_test.go (about) 1 package process 2 3 import ( 4 "testing" 5 6 "github.com/google/go-cmp/cmp" 7 "github.com/grafana/tanka/pkg/kubernetes/manifest" 8 "github.com/grafana/tanka/pkg/spec/v1alpha1" 9 "github.com/stretchr/testify/require" 10 ) 11 12 func TestProcess(t *testing.T) { 13 tests := []struct { 14 name string 15 spec v1alpha1.Spec 16 17 deep interface{} 18 flat manifest.List 19 20 targets Matchers 21 err error 22 }{ 23 { 24 name: "regular", 25 deep: testDataRegular().Deep, 26 flat: mapToList(testDataRegular().Flat), 27 }, 28 { 29 name: "injectLabels", 30 deep: testDataRegular().Deep, 31 flat: mapToList(testDataRegular().Flat), 32 spec: v1alpha1.Spec{ 33 InjectLabels: true, 34 }, 35 }, 36 { 37 name: "targets", 38 deep: testDataDeep().Deep, 39 flat: manifest.List{ 40 testDataDeep().Flat[".app.web.backend.server.grafana.deployment"], 41 testDataDeep().Flat[".app.web.frontend.nodejs.express.service"], 42 }, 43 targets: MustStrExps( 44 `deployment/grafana`, 45 `service/frontend`, 46 ), 47 }, 48 { 49 name: "targets-regex", 50 deep: testDataDeep().Deep, 51 flat: manifest.List{ 52 testDataDeep().Flat[".app.web.backend.server.grafana.deployment"], 53 testDataDeep().Flat[".app.web.frontend.nodejs.express.deployment"], 54 }, 55 targets: MustStrExps(`deployment/.*`), 56 }, 57 { 58 name: "targets-caseInsensitive", 59 deep: testDataDeep().Deep, 60 flat: manifest.List{ 61 testDataDeep().Flat[".app.web.backend.server.grafana.deployment"], 62 }, 63 targets: MustStrExps( 64 `DePlOyMeNt/GrAfAnA`, 65 ), 66 }, 67 { 68 name: "targets-negative", 69 deep: testDataDeep().Deep, 70 flat: manifest.List{ 71 testDataDeep().Flat[".app.web.frontend.nodejs.express.service"], 72 testDataDeep().Flat[".app.namespace"], 73 }, 74 targets: MustStrExps(`!deployment/.*`), 75 }, 76 { 77 name: "unwrap-list", 78 deep: loadFixture("tdList.jsonnet").Deep, 79 flat: manifest.List{ 80 loadFixture("tdList.jsonnet").Flat["foo.items[0]"], 81 loadFixture("tdList.jsonnet").Flat["foo.items[1]"], 82 }, 83 }, 84 { 85 name: "force-namespace", 86 spec: v1alpha1.Spec{Namespace: "tanka"}, 87 deep: testDataFlat().Deep, 88 flat: func() manifest.List { 89 f := testDataFlat().Flat["."] 90 f.Metadata()["namespace"] = "tanka" 91 return manifest.List{f} 92 }(), 93 }, 94 { 95 name: "custom-namespace", 96 spec: v1alpha1.Spec{Namespace: "tanka"}, 97 deep: func() map[string]interface{} { 98 d := testDataFlat().Deep.(map[string]interface{}) 99 d["metadata"].(map[string]interface{})["namespace"] = "custom" 100 return d 101 }(), 102 flat: func() manifest.List { 103 f := testDataFlat().Flat["."] 104 f.Metadata()["namespace"] = "custom" 105 return manifest.List{f} 106 }(), 107 }, 108 } 109 110 for _, c := range tests { 111 t.Run(c.name, func(t *testing.T) { 112 env := v1alpha1.New() 113 env.Metadata.Name = "testdata" 114 env.Spec = c.spec 115 env.Data = c.deep.(map[string]interface{}) 116 117 if env.Spec.InjectLabels { 118 for i, m := range c.flat { 119 m.Metadata().Labels()[LabelEnvironment] = env.Metadata.NameLabel() 120 c.flat[i] = m 121 } 122 } 123 124 got, err := Process(*env, c.targets) 125 require.Equal(t, c.err, err) 126 127 Sort(c.flat) 128 if s := cmp.Diff(c.flat, got); s != "" { 129 t.Error(s) 130 } 131 }) 132 } 133 } 134 135 func mapToList(ms map[string]manifest.Manifest) manifest.List { 136 l := make(manifest.List, 0, len(ms)) 137 for _, m := range ms { 138 l = append(l, m) 139 } 140 return l 141 } 142 143 func TestProcessOrder(t *testing.T) { 144 got := make([]manifest.List, 10) 145 for i := 0; i < 10; i++ { 146 env := v1alpha1.New() 147 env.Data = testDataDeep().Deep.(map[string]interface{}) 148 149 r, err := Process(*env, nil) 150 require.NoError(t, err) 151 got[i] = r 152 } 153 154 for i := 1; i < 10; i++ { 155 require.Equal(t, got[0], got[i]) 156 } 157 }