github.com/Axway/agent-sdk@v1.1.101/pkg/apic/apiserver/clients/api/v1/client_srv_test.go (about) 1 //go:build withapiserver 2 // +build withapiserver 3 4 // Tests in this file are not run by default 5 // They need an apiserver started on localhost:8080 6 // to run apply the "withapiserver" tag to the test command: 7 // go test --tags withapiserver 8 9 package v1_test 10 11 import ( 12 "context" 13 "fmt" 14 "reflect" 15 "testing" 16 "time" 17 18 . "github.com/Axway/agent-sdk/pkg/apic/apiserver/clients/api/v1" 19 v1 "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/api/v1" 20 management "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/management/v1alpha1" 21 ) 22 23 func TestQueries(t *testing.T) { 24 cb := NewClient( 25 "http://localhost:8080/apis", 26 UserAgent("fake-agent"), 27 BasicAuth( 28 "admin", 29 "servicesecret", 30 "admin", "123", 31 ), 32 ) 33 34 cEnv, err := cb.ForKind(management.EnvironmentGVK()) 35 if err != nil { 36 t.Fatalf("Failed due: %s", err) 37 } 38 39 env1, err := cEnv.Create(context.Background(), &v1.ResourceInstance{ 40 ResourceMeta: v1.ResourceMeta{ 41 Name: fmt.Sprintf("env1.%d", time.Now().Unix()), 42 Attributes: map[string]string{ 43 "attr1": "val1", 44 "attr": "val", 45 "diffattr": "val1", 46 }, 47 Tags: []string{ 48 "tag", "tag1", 49 }, 50 }, 51 Spec: map[string]interface{}{}, 52 }) 53 if err != nil { 54 t.Fatalf("Failed due: %s", err) 55 } 56 defer func() { 57 cEnv.Delete(context.Background(), env1) 58 }() 59 60 env2, err := cEnv.Create(context.Background(), &v1.ResourceInstance{ 61 ResourceMeta: v1.ResourceMeta{ 62 Name: fmt.Sprintf("env2.%d", time.Now().Unix()), 63 Attributes: map[string]string{ 64 "attr2": "val2", 65 "attr": "val", 66 "diffattr": "val2", 67 }, 68 Tags: []string{ 69 "tag", "tag2", 70 }, 71 }, 72 Spec: map[string]interface{}{}, 73 }) 74 if err != nil { 75 t.Fatalf("Failed due: %s", err) 76 } 77 defer func() { 78 cEnv.Delete(context.Background(), env2) 79 }() 80 81 testCases := []struct { 82 name string 83 query QueryNode 84 expected []string 85 }{{ 86 "common attribute and value", 87 AttrIn("attr", "val"), 88 []string{env1.Name, env2.Name}, 89 }, { 90 "common tag", 91 TagsIn("tag"), 92 []string{env1.Name, env2.Name}, 93 }, { 94 "tag with one match", 95 TagsIn("tag1"), 96 []string{env1.Name}, 97 }, { 98 "two tags", 99 TagsIn("tag1", "tag2"), 100 []string{env1.Name, env2.Name}, 101 }, { 102 "attribute with two values", 103 AttrIn("diffattr", "val1"), 104 []string{env1.Name}, 105 }, { 106 "any attr", 107 AnyAttr(map[string]string{"attr1": "val1", "attr2": "val2"}), 108 []string{env1.Name, env2.Name}, 109 }, { 110 "all attr", 111 AllAttr(map[string]string{"attr1": "val1", "diffattr": "val1"}), 112 []string{env1.Name}, 113 }, { 114 "all attr and one tag", 115 And(AllAttr(map[string]string{"attr1": "val1", "diffattr": "val1"}), TagsIn("tag")), 116 []string{env1.Name}, 117 }, { 118 "all attr and one tag no result", 119 And(AllAttr(map[string]string{"attr1": "val1", "diffattr": "val1"}), TagsIn("tag2")), 120 []string{}, 121 }, { 122 "all attr or one tag", 123 Or(AllAttr(map[string]string{"attr1": "val1", "diffattr": "val1"}), TagsIn("tag2")), 124 []string{env1.Name, env2.Name}, 125 }, 126 } 127 128 for i, _ := range testCases { 129 tc := testCases[i] 130 t.Run(tc.name, func(t *testing.T) { 131 ris, err := cEnv.List(context.Background(), WithQuery(tc.query)) 132 if err != nil { 133 t.Errorf("Failed due: %s", err) 134 } 135 136 names := make([]string, len(ris)) 137 138 for i, ri := range ris { 139 names[i] = ri.Name 140 } 141 142 if !reflect.DeepEqual(tc.expected, names) { 143 t.Errorf("Gots %+v, expected %+v", names, tc.expected) 144 } 145 }) 146 } 147 }