github.com/caos/orbos@v1.5.14-0.20221103111702-e6cd0cea7ad4/internal/operator/boom/crd/crd_test.go (about) 1 package crd 2 3 import ( 4 "os" 5 "testing" 6 7 "github.com/caos/orbos/internal/operator/boom/api/migrate" 8 "github.com/caos/orbos/internal/operator/boom/api/v1beta1/argocd" 9 "github.com/caos/orbos/internal/operator/boom/api/v1beta1/grafana" 10 11 "github.com/caos/orbos/internal/operator/boom/api/v1beta1" 12 application "github.com/caos/orbos/internal/operator/boom/application/mock" 13 "github.com/caos/orbos/internal/operator/boom/bundle" 14 "github.com/caos/orbos/internal/operator/boom/bundle/bundles" 15 bundleconfig "github.com/caos/orbos/internal/operator/boom/bundle/config" 16 "github.com/caos/orbos/internal/operator/boom/crd/config" 17 "github.com/caos/orbos/internal/operator/boom/name" 18 "github.com/caos/orbos/internal/operator/boom/templator/yaml" 19 "github.com/caos/orbos/internal/utils/clientgo" 20 "github.com/caos/orbos/internal/utils/helper" 21 "github.com/caos/orbos/mntr" 22 "github.com/stretchr/testify/assert" 23 _ "k8s.io/client-go/plugin/pkg/client/auth/gcp" 24 ) 25 26 var ( 27 fullToolset = &v1beta1.Toolset{ 28 Metadata: &v1beta1.Metadata{ 29 Name: "caos_test", 30 }, 31 Spec: &v1beta1.ToolsetSpec{ 32 Ambassador: &v1beta1.Ambassador{ 33 Deploy: true, 34 }, 35 Argocd: &argocd.Argocd{ 36 Deploy: true, 37 }, 38 KubeStateMetrics: &v1beta1.KubeStateMetrics{ 39 Deploy: true, 40 }, 41 PrometheusOperator: &v1beta1.PrometheusOperator{ 42 Deploy: true, 43 }, 44 PrometheusNodeExporter: &v1beta1.PrometheusNodeExporter{ 45 Deploy: true, 46 }, 47 Grafana: &grafana.Grafana{ 48 Deploy: true, 49 }, 50 }, 51 } 52 changedToolset = &v1beta1.Toolset{ 53 Metadata: &v1beta1.Metadata{ 54 Name: "caos_test", 55 }, 56 Spec: &v1beta1.ToolsetSpec{ 57 Ambassador: &v1beta1.Ambassador{ 58 Deploy: false, 59 }, 60 Argocd: &argocd.Argocd{ 61 Deploy: true, 62 }, 63 KubeStateMetrics: &v1beta1.KubeStateMetrics{ 64 Deploy: true, 65 }, 66 PrometheusOperator: &v1beta1.PrometheusOperator{ 67 Deploy: true, 68 }, 69 PrometheusNodeExporter: &v1beta1.PrometheusNodeExporter{ 70 Deploy: true, 71 }, 72 Grafana: &grafana.Grafana{ 73 Deploy: true, 74 }, 75 }, 76 } 77 78 testHelperResource = &helper.Resource{ 79 Kind: "test", 80 ApiVersion: "test/v1", 81 Metadata: &helper.Metadata{ 82 Name: "test", 83 Namespace: "test", 84 }, 85 } 86 testClientgoResource = &clientgo.Resource{ 87 Group: "test", 88 Version: "v1", 89 Resource: "test", 90 Kind: "test", 91 Name: "test", 92 Namespace: "test", 93 Labels: map[string]string{"test": "test"}, 94 } 95 ) 96 97 func newCrd() *Crd { 98 99 monitor := mntr.Monitor{ 100 OnInfo: mntr.LogMessage, 101 OnChange: mntr.LogMessage, 102 OnError: mntr.LogError, 103 } 104 105 conf := &config.Config{ 106 Monitor: monitor, 107 } 108 109 return New(conf) 110 } 111 112 func setBundle(c *Crd, bundle name.Bundle) func() { 113 basePath := "/tmp/crd_test" 114 os.MkdirAll(basePath, os.ModePerm) 115 116 monitor := mntr.Monitor{ 117 OnInfo: mntr.LogMessage, 118 OnChange: mntr.LogMessage, 119 OnError: mntr.LogError, 120 } 121 122 bundleConfig := &bundleconfig.Config{ 123 Monitor: monitor, 124 CrdName: "caos_test", 125 BundleName: bundle, 126 BaseDirectoryPath: basePath, 127 Templator: yaml.GetName(), 128 } 129 130 c.SetBundle(bundleConfig) 131 132 return func() { 133 os.RemoveAll(basePath) 134 } 135 } 136 137 func init() { 138 bundle.Testmode = true 139 } 140 141 func TestNew(t *testing.T) { 142 crd := newCrd() 143 assert.NotNil(t, crd) 144 } 145 146 func TestNew_noexistendbundle(t *testing.T) { 147 var nonexistent name.Bundle 148 nonexistent = "nonexistent" 149 crd := newCrd() 150 clean := setBundle(crd, nonexistent) 151 defer clean() 152 assert.Error(t, crd.GetStatus()) 153 assert.NotNil(t, crd) 154 } 155 156 func TestCrd_Reconcile_initial(t *testing.T) { 157 crd := newCrd() 158 clean := setBundle(crd, bundles.Empty) 159 defer clean() 160 bundle := crd.GetBundle() 161 162 app := application.NewTestYAMLApplication(t) 163 app.SetDeploy(fullToolset.Spec, true).SetGetYaml(fullToolset.Spec, "test") 164 bundle.AddApplication(app.Application()) 165 assert.NotNil(t, crd) 166 167 // when crd is nil 168 resources := []*clientgo.Resource{testClientgoResource} 169 crd.Reconcile(resources, migrate.V1beta1Tolatest(fullToolset)) 170 err := crd.GetStatus() 171 assert.NoError(t, err) 172 } 173 174 func TestCrd_Reconcile_changed(t *testing.T) { 175 crd := newCrd() 176 clean := setBundle(crd, bundles.Empty) 177 defer clean() 178 bundle := crd.GetBundle() 179 180 app := application.NewTestYAMLApplication(t) 181 app.SetDeploy(fullToolset.Spec, true).SetGetYaml(fullToolset.Spec, "test") 182 bundle.AddApplication(app.Application()) 183 assert.NotNil(t, crd) 184 185 // when crd is nil 186 resources := []*clientgo.Resource{testClientgoResource} 187 crd.Reconcile(resources, migrate.V1beta1Tolatest(fullToolset)) 188 err := crd.GetStatus() 189 assert.NoError(t, err) 190 191 //changed crd 192 app.SetDeploy(changedToolset.Spec, true).SetGetYaml(changedToolset.Spec, "test2") 193 crd.Reconcile(resources, migrate.V1beta1Tolatest(changedToolset)) 194 err = crd.GetStatus() 195 assert.NoError(t, err) 196 } 197 198 func TestCrd_Reconcile_changedDelete(t *testing.T) { 199 crd := newCrd() 200 clean := setBundle(crd, bundles.Empty) 201 defer clean() 202 bundle := crd.GetBundle() 203 204 app := application.NewTestYAMLApplication(t) 205 app.SetDeploy(fullToolset.Spec, true).SetGetYaml(fullToolset.Spec, "test") 206 bundle.AddApplication(app.Application()) 207 assert.NotNil(t, crd) 208 209 // when crd is nil 210 resources := []*clientgo.Resource{testClientgoResource} 211 crd.Reconcile(resources, migrate.V1beta1Tolatest(fullToolset)) 212 err := crd.GetStatus() 213 assert.NoError(t, err) 214 215 //changed crd 216 app.SetDeploy(changedToolset.Spec, false).SetGetYaml(changedToolset.Spec, "test2") 217 crd.Reconcile(resources, migrate.V1beta1Tolatest(changedToolset)) 218 err = crd.GetStatus() 219 assert.NoError(t, err) 220 } 221 222 func TestCrd_Reconcile_initialNotDeployed(t *testing.T) { 223 crd := newCrd() 224 clean := setBundle(crd, bundles.Empty) 225 defer clean() 226 bundle := crd.GetBundle() 227 228 app := application.NewTestYAMLApplication(t) 229 app.SetDeploy(fullToolset.Spec, false).SetGetYaml(fullToolset.Spec, "test") 230 bundle.AddApplication(app.Application()) 231 assert.NotNil(t, crd) 232 233 // when crd is nil 234 resources := []*clientgo.Resource{testClientgoResource} 235 crd.Reconcile(resources, migrate.V1beta1Tolatest(fullToolset)) 236 err := crd.GetStatus() 237 assert.NoError(t, err) 238 239 //changed crd 240 app.SetDeploy(changedToolset.Spec, false).SetGetYaml(changedToolset.Spec, "test2") 241 crd.Reconcile(resources, migrate.V1beta1Tolatest(changedToolset)) 242 err = crd.GetStatus() 243 assert.NoError(t, err) 244 }