-
Notifications
You must be signed in to change notification settings - Fork 75
/
matchers.go
112 lines (92 loc) · 3.37 KB
/
matchers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright (c) 2020 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package infrastructure
import (
"fmt"
"net/http"
"reflect"
"github.com/Azure/go-autorest/autorest"
"github.com/onsi/gomega/format"
"github.com/onsi/gomega/types"
)
type azureNotFoundErrorMatcher struct{}
func (a *azureNotFoundErrorMatcher) Match(actual interface{}) (success bool, err error) {
if actual == nil {
return false, nil
}
azError, ok := actual.(autorest.DetailedError)
if !ok {
return false, fmt.Errorf("expected autorest.Detailed error, got %s", format.Object(actual, 1))
}
code, ok := azError.StatusCode.(int)
if !ok {
return false, fmt.Errorf("could not parse response status code")
}
return code == http.StatusNotFound, nil
}
func (a *azureNotFoundErrorMatcher) FailureMessage(actual interface{}) (message string) {
return format.Message(actual, "to be not found error")
}
func (a *azureNotFoundErrorMatcher) NegatedFailureMessage(actual interface{}) (message string) {
return format.Message(actual, "to not be not found error")
}
// BeNotFoundError matches errors returned by azure-sdk-for-go remote calls when an object could not be found (HTTP Status code = 404).
func BeNotFoundError() types.GomegaMatcher {
return &azureNotFoundErrorMatcher{}
}
type azureIDMatcher struct {
expected string
}
func (a azureIDMatcher) Match(actual interface{}) (success bool, err error) {
defer func() {
if panicErr := recover(); panicErr != nil {
success = false
err = fmt.Errorf("panicked while matching ID, got:\n%s", panicErr)
}
}()
if actual == nil {
return false, nil
}
val := reflect.ValueOf(actual)
if val.Kind() == reflect.Ptr {
val = val.Elem()
}
if val.Kind() != reflect.Struct {
return false, fmt.Errorf("expected struct or pointer, got %s", format.Object(actual, 1))
}
idField := val.FieldByName("ID")
if !idField.IsValid() {
return false, fmt.Errorf("ID field not found")
}
var id string
if idField.Kind() == reflect.Ptr {
id = idField.Elem().String()
} else {
id = idField.String()
}
return id == a.expected, nil
}
func (a azureIDMatcher) FailureMessage(actual interface{}) (message string) {
return format.Message(actual, "to match ID")
}
func (a azureIDMatcher) NegatedFailureMessage(actual interface{}) (message string) {
return format.Message(actual, "to not match ID")
}
// HaveEqualID succeeds if Azure object has the expected ID.
// Azure objects are identified by an ID, which in azure-sdk-for-go is mapped to an `ID *string` field.
// HaveEqualID will succeed if actual is a struct or a pointer to a struct containing a field with this specification and
// points to a string equal to expected.
func HaveEqualID(expected string) types.GomegaMatcher {
return &azureIDMatcher{expected: expected}
}