|
| 1 | +package recipe |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 7 | + types "mayadata.io/d-operators/types/recipe" |
| 8 | +) |
| 9 | + |
| 10 | +func TestPathCheckingAssertValueString(t *testing.T) { |
| 11 | + var tests = map[string]struct { |
| 12 | + State *unstructured.Unstructured |
| 13 | + TaskName string |
| 14 | + PathCheck string |
| 15 | + Path string |
| 16 | + Value string |
| 17 | + retryIfValueEquals bool |
| 18 | + retryIfValueNotEquals bool |
| 19 | + IsError bool |
| 20 | + ExpectedAssert bool |
| 21 | + }{ |
| 22 | + "assert ipaddress None == None": { |
| 23 | + State: &unstructured.Unstructured{ |
| 24 | + Object: map[string]interface{}{ |
| 25 | + "apiVersion": "v1", |
| 26 | + "kind": "Service", |
| 27 | + "metadata": map[string]interface{}{ |
| 28 | + "name": "test", |
| 29 | + "namespace": "default", |
| 30 | + }, |
| 31 | + "spec": map[string]interface{}{ |
| 32 | + "ipAddress": "None", |
| 33 | + }, |
| 34 | + }, |
| 35 | + }, |
| 36 | + Path: "spec.ipAddress", |
| 37 | + Value: "None", |
| 38 | + retryIfValueNotEquals: true, |
| 39 | + ExpectedAssert: true, |
| 40 | + }, |
| 41 | + "assert ipaddress ip != 12.123.12.11": { |
| 42 | + State: &unstructured.Unstructured{ |
| 43 | + Object: map[string]interface{}{ |
| 44 | + "apiVersion": "v1", |
| 45 | + "kind": "Service", |
| 46 | + "metadata": map[string]interface{}{ |
| 47 | + "name": "test", |
| 48 | + "namespace": "default", |
| 49 | + }, |
| 50 | + "spec": map[string]interface{}{ |
| 51 | + "ipAddress": "12.123.12.11", |
| 52 | + }, |
| 53 | + }, |
| 54 | + }, |
| 55 | + Path: "spec.ipAddress", |
| 56 | + Value: "None", |
| 57 | + retryIfValueEquals: true, |
| 58 | + ExpectedAssert: true, |
| 59 | + }, |
| 60 | + "assert ipaddress ip != ": { |
| 61 | + State: &unstructured.Unstructured{ |
| 62 | + Object: map[string]interface{}{ |
| 63 | + "apiVersion": "v1", |
| 64 | + "kind": "Service", |
| 65 | + "metadata": map[string]interface{}{ |
| 66 | + "name": "test", |
| 67 | + "namespace": "default", |
| 68 | + }, |
| 69 | + "spec": map[string]interface{}{ |
| 70 | + "ipAddress": "12.123.12.11", |
| 71 | + }, |
| 72 | + }, |
| 73 | + }, |
| 74 | + Path: "spec.ipAddress", |
| 75 | + Value: "", |
| 76 | + retryIfValueEquals: true, |
| 77 | + ExpectedAssert: true, |
| 78 | + }, |
| 79 | + "assert ipaddress Nil != None": { |
| 80 | + State: &unstructured.Unstructured{ |
| 81 | + Object: map[string]interface{}{ |
| 82 | + "apiVersion": "v1", |
| 83 | + "kind": "Service", |
| 84 | + "metadata": map[string]interface{}{ |
| 85 | + "name": "test", |
| 86 | + "namespace": "default", |
| 87 | + }, |
| 88 | + "spec": map[string]interface{}{ |
| 89 | + "ipAddress": "None", |
| 90 | + }, |
| 91 | + }, |
| 92 | + }, |
| 93 | + Path: "spec.ipAddress", |
| 94 | + Value: "Nil", |
| 95 | + retryIfValueNotEquals: true, |
| 96 | + ExpectedAssert: false, |
| 97 | + }, |
| 98 | + } |
| 99 | + for scenario, tObj := range tests { |
| 100 | + scenario := scenario |
| 101 | + tObj := tObj |
| 102 | + t.Run(scenario, func(t *testing.T) { |
| 103 | + pc := &PathChecking{ |
| 104 | + PathCheck: types.PathCheck{ |
| 105 | + Path: tObj.Path, |
| 106 | + Value: tObj.Value, |
| 107 | + }, |
| 108 | + retryIfValueEquals: tObj.retryIfValueEquals, |
| 109 | + retryIfValueNotEquals: tObj.retryIfValueNotEquals, |
| 110 | + result: &types.PathCheckResult{}, |
| 111 | + } |
| 112 | + got, err := pc.assertValueString(tObj.State) |
| 113 | + if tObj.IsError && err == nil { |
| 114 | + t.Fatalf("Expected error got none") |
| 115 | + } |
| 116 | + if !tObj.IsError && err != nil { |
| 117 | + t.Fatalf("Expected no error got %s", err.Error()) |
| 118 | + } |
| 119 | + if tObj.IsError { |
| 120 | + return |
| 121 | + } |
| 122 | + if got != tObj.ExpectedAssert { |
| 123 | + t.Fatalf("Expected assert = %t got %t", tObj.ExpectedAssert, got) |
| 124 | + } |
| 125 | + |
| 126 | + }) |
| 127 | + } |
| 128 | +} |
0 commit comments