github.com/caos/orbos@v1.5.14-0.20221103111702-e6cd0cea7ad4/internal/operator/orbiter/kinds/loadbalancers/dynamic/desired_test.go (about) 1 package dynamic 2 3 import ( 4 "errors" 5 "testing" 6 7 "github.com/caos/orbos/pkg/tree" 8 "gopkg.in/yaml.v3" 9 ) 10 11 func TestMigration(t *testing.T) { 12 13 noWlTransport := map[string]interface{}{ 14 "name": "ip1", 15 "sourceport": 3000, 16 "destinations": []map[string]interface{}{{ 17 "healthchecks": map[string]interface{}{ 18 "protocol": "http", 19 "path": "/", 20 "code": 200, 21 }, 22 "port": 33000, 23 "pool": "dummy", 24 }}, 25 } 26 27 tests := []struct { 28 name string 29 version string 30 spec interface{} 31 check func(*Desired) error 32 wantUnmarshalErr bool 33 }{{ 34 name: "Whitelists should be migrated from v0 to v1", 35 version: "v0", 36 spec: map[string][]map[string]interface{}{ 37 "pool": {{ 38 "ip": "192.168.0.10", 39 "whitelist": []string{ 40 "127.168.0.20/32", 41 "127.168.0.21/32", 42 }, 43 "transport": []map[string]interface{}{noWlTransport, noWlTransport}, 44 }, { 45 "ip": "192.168.0.11", 46 "whitelist": []string{ 47 "127.168.0.20/32", 48 "127.168.0.21/32", 49 "127.168.0.22/32", 50 }, 51 "transport": []map[string]interface{}{noWlTransport, noWlTransport}, 52 }}, 53 }, 54 check: func(desired *Desired) error { 55 if desired.Common.Version != "v1" { 56 return errors.New("Version not incremented") 57 } 58 if len(desired.Spec["pool"][0].Transport[0].Whitelist) != 2 { 59 return errors.New("Whitelist not correctly moved") 60 } 61 if len(desired.Spec["pool"][0].Transport[1].Whitelist) != 2 { 62 return errors.New("Whitelist not correctly moved") 63 } 64 if len(desired.Spec["pool"][1].Transport[0].Whitelist) != 3 { 65 return errors.New("Whitelist not correctly moved") 66 } 67 return nil 68 }, 69 wantUnmarshalErr: false, 70 }, { 71 name: "V2 should not be supported", 72 version: "v2", 73 spec: map[string][]*interface{}{}, 74 check: func(desired *Desired) error { return nil }, 75 wantUnmarshalErr: true, 76 }} 77 78 for _, tt := range tests { 79 t.Run(tt.name, func(t *testing.T) { 80 template, err := yaml.Marshal(map[string]interface{}{"spec": tt.spec}) 81 if err != nil { 82 t.Fatal(err) 83 } 84 85 d := &Desired{Common: &tree.Common{Version: tt.version}} 86 if unmarshalErr := yaml.Unmarshal(template, d); (unmarshalErr != nil) != tt.wantUnmarshalErr { 87 t.Errorf("%s\nyaml.Unmarshal() error = %v, wantUnmarshalErr %v", string(template), unmarshalErr, tt.wantUnmarshalErr) 88 } 89 if err := tt.check(d); err != nil { 90 t.Errorf("%s\nerror = %v", string(template), err) 91 } 92 }) 93 } 94 }