github.com/zntrio/harp/v2@v2.0.9/pkg/bundle/ruleset/ruleset_test.go (about) 1 // Licensed to Elasticsearch B.V. under one or more contributor 2 // license agreements. See the NOTICE file distributed with 3 // this work for additional information regarding copyright 4 // ownership. Elasticsearch B.V. licenses this file to you under 5 // the Apache License, Version 2.0 (the "License"); you may 6 // not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, 12 // software distributed under the License is distributed on an 13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 // KIND, either express or implied. See the License for the 15 // specific language governing permissions and limitations 16 // under the License. 17 18 package ruleset 19 20 import ( 21 "reflect" 22 "testing" 23 24 bundlev1 "github.com/zntrio/harp/v2/api/gen/go/harp/bundle/v1" 25 ) 26 27 func TestValidate(t *testing.T) { 28 type args struct { 29 spec *bundlev1.RuleSet 30 } 31 tests := []struct { 32 name string 33 args args 34 wantErr bool 35 }{ 36 { 37 name: "nil", 38 wantErr: true, 39 }, 40 { 41 name: "invalid apiVersion", 42 args: args{ 43 spec: &bundlev1.RuleSet{ 44 ApiVersion: "foo", 45 }, 46 }, 47 wantErr: true, 48 }, 49 { 50 name: "invalid kind", 51 args: args{ 52 spec: &bundlev1.RuleSet{ 53 ApiVersion: "harp.elastic.co/v1", 54 Kind: "foo", 55 }, 56 }, 57 wantErr: true, 58 }, 59 { 60 name: "nil meta", 61 args: args{ 62 spec: &bundlev1.RuleSet{ 63 ApiVersion: "harp.elastic.co/v1", 64 Kind: "RuleSet", 65 }, 66 }, 67 wantErr: true, 68 }, 69 { 70 name: "meta name not defined", 71 args: args{ 72 spec: &bundlev1.RuleSet{ 73 ApiVersion: "harp.elastic.co/v1", 74 Kind: "RuleSet", 75 Meta: &bundlev1.RuleSetMeta{}, 76 }, 77 }, 78 wantErr: true, 79 }, 80 { 81 name: "nil spec", 82 args: args{ 83 spec: &bundlev1.RuleSet{ 84 ApiVersion: "harp.elastic.co/v1", 85 Kind: "RuleSet", 86 Meta: &bundlev1.RuleSetMeta{}, 87 }, 88 }, 89 wantErr: true, 90 }, 91 { 92 name: "no action patch", 93 args: args{ 94 spec: &bundlev1.RuleSet{ 95 ApiVersion: "harp.elastic.co/v1", 96 Kind: "RuleSet", 97 Meta: &bundlev1.RuleSetMeta{}, 98 Spec: &bundlev1.RuleSetSpec{}, 99 }, 100 }, 101 wantErr: false, 102 }, 103 } 104 for _, tt := range tests { 105 t.Run(tt.name, func(t *testing.T) { 106 if err := Validate(tt.args.spec); (err != nil) != tt.wantErr { 107 t.Errorf("Validate() error = %v, wantErr %v", err, tt.wantErr) 108 } 109 }) 110 } 111 } 112 113 func TestChecksum(t *testing.T) { 114 type args struct { 115 spec *bundlev1.RuleSet 116 } 117 tests := []struct { 118 name string 119 args args 120 want string 121 wantErr bool 122 }{ 123 { 124 name: "nil", 125 wantErr: true, 126 }, 127 { 128 name: "valid", 129 args: args{ 130 spec: &bundlev1.RuleSet{ 131 ApiVersion: "harp.elastic.co/v1", 132 Kind: "RuleSet", 133 Meta: &bundlev1.RuleSetMeta{}, 134 Spec: &bundlev1.RuleSetSpec{}, 135 }, 136 }, 137 wantErr: false, 138 want: "yM_TR6rMWW7BGA1Ms-U3WK6E4Xax5qRBMjK4VQLyZmQ", 139 }, 140 } 141 for _, tt := range tests { 142 t.Run(tt.name, func(t *testing.T) { 143 got, err := Checksum(tt.args.spec) 144 if (err != nil) != tt.wantErr { 145 t.Errorf("Checksum() error = %v, wantErr %v", err, tt.wantErr) 146 return 147 } 148 if !reflect.DeepEqual(got, tt.want) { 149 t.Errorf("Checksum() = %v, want %v", got, tt.want) 150 } 151 }) 152 } 153 }