This repository has been archived by the owner on Feb 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathtestenv_test.go
120 lines (101 loc) · 2.67 KB
/
testenv_test.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
113
114
115
116
117
118
119
120
package testenv
import (
"flag"
"io"
"log"
"os"
"path"
"testing"
"github.com/pborman/uuid"
"golang.org/x/net/context"
mixerapi "istio.io/api/mixer/v1"
"istio.io/mixer/adapter/denier"
"istio.io/mixer/pkg/adapter"
"istio.io/mixer/template"
)
func closeHelper(c io.Closer) {
err := c.Close()
if err != nil {
log.Fatal(err)
}
}
func copyFile(dest, src string) error {
in, err := os.Open(src)
if err != nil {
return err
}
defer closeHelper(in)
out, err := os.Create(dest)
if err != nil {
return err
}
defer closeHelper(out)
_, err = io.Copy(out, in)
return err
}
func buildConfigStore(relativePaths []string) (string, error) {
currentPath, err := os.Getwd()
if err != nil {
return "", err
}
configPath := path.Join(currentPath, uuid.New())
if err = os.Mkdir(configPath, os.ModePerm); err != nil {
return "", err
}
for _, filePath := range relativePaths {
err = copyFile(path.Join(configPath, path.Base(filePath)), path.Join(currentPath, filePath))
if err != nil {
return "", err
}
}
return configPath, nil
}
func TestMain(m *testing.M) {
flag.Parse()
code := m.Run()
os.Exit(code)
}
func TestDenierAdapter(t *testing.T) {
configStore, err := buildConfigStore([]string{
"../../testdata/config/attributes.yaml",
"../../testdata/config/deny.yaml",
})
if err != nil {
t.Fatalf("fail to build test config store: %v", err)
}
defer func() {
if removeErr := os.RemoveAll(configStore); removeErr != nil {
log.Fatal(removeErr)
}
}()
var args = Args{
// Start Mixer server on a free port on loop back interface
MixerServerAddr: `127.0.0.1:0`,
ConfigStoreURL: `fs://` + configStore,
ConfigStore2URL: `fs://` + configStore,
ConfigDefaultNamespace: "istio-system",
ConfigIdentityAttribute: "destination.service",
ConfigIdentityAttributeDomain: "svc.cluster.local",
UseAstEvaluator: true,
}
env, err := NewEnv(&args, template.SupportedTmplInfo, []adapter.InfoFn{denier.GetInfo})
if err != nil {
t.Fatalf("fail to create testenv: %v", err)
}
defer closeHelper(env)
client, conn, err := env.CreateMixerClient()
if err != nil {
t.Fatalf("fail to create client connection: %v", err)
}
defer closeHelper(conn)
attrs := map[string]interface{}{"request.headers": map[string]string{"clnt": "abc"}}
bag := GetAttrBag(attrs, args.ConfigIdentityAttribute, args.ConfigIdentityAttributeDomain)
request := mixerapi.CheckRequest{Attributes: bag}
resq, err := client.Check(context.Background(), &request)
if err != nil {
t.Errorf("fail to send check to Mixer %v", err)
}
if resq.Precondition.Status.Code != 7 {
t.Error(`resq.Precondition.Status.Code != 7`)
}
}