1// Copyright 2017 Istio Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package denier
16
17// NOTE: This test will eventually be auto-generated so that it automatically supports all CHECK and QUOTA
18//       templates known to Mixer. For now, it's manually curated.
19
20import (
21	"context"
22	"reflect"
23	"testing"
24
25	"istio.io/istio/mixer/pkg/adapter"
26	"istio.io/istio/mixer/pkg/adapter/test"
27	"istio.io/istio/mixer/template/checknothing"
28	"istio.io/istio/mixer/template/listentry"
29	"istio.io/istio/mixer/template/quota"
30)
31
32func TestBasic(t *testing.T) {
33	info := GetInfo()
34
35	if !contains(info.SupportedTemplates, checknothing.TemplateName) ||
36		!contains(info.SupportedTemplates, listentry.TemplateName) ||
37		!contains(info.SupportedTemplates, quota.TemplateName) {
38		t.Error("Didn't find all expected supported templates")
39	}
40
41	b := info.NewBuilder().(*builder)
42	b.SetCheckNothingTypes(nil)
43	b.SetListEntryTypes(nil)
44	b.SetQuotaTypes(nil)
45	b.SetAdapterConfig(info.DefaultConfig)
46
47	if err := b.Validate(); err != nil {
48		t.Errorf("Got error %v, expecting success", err)
49	}
50
51	handler, err := b.Build(context.Background(), test.NewEnv(t))
52	if err != nil {
53		t.Errorf("Got error %v, expecting success", err)
54	}
55
56	checkNothingHandler := handler.(checknothing.Handler)
57	result, err := checkNothingHandler.HandleCheckNothing(context.Background(), nil)
58	if err != nil {
59		t.Errorf("Got error %v, expecting success", err)
60	}
61
62	want := newResult(defaultParam())
63
64	if !reflect.DeepEqual(want, result) {
65		t.Errorf("Got %v, expected %v", result, want)
66	}
67
68	listEntryHandler := handler.(listentry.Handler)
69	result, err = listEntryHandler.HandleListEntry(context.Background(), nil)
70	if err != nil {
71		t.Errorf("Got error %v, expecting success", err)
72	}
73
74	if !reflect.DeepEqual(want, result) {
75		t.Errorf("Got %v, expected %v", result, want)
76	}
77
78	quotaHandler := handler.(quota.Handler)
79	qr, err := quotaHandler.HandleQuota(context.Background(), nil, adapter.QuotaArgs{QuotaAmount: 100})
80	if err != nil {
81		t.Errorf("Got error %v, expecting success", err)
82	}
83
84	if qr.Amount != 0 {
85		t.Errorf("Got %d quota, expecting 0", qr.Amount)
86	}
87
88	if qr.ValidDuration != 0 {
89		t.Errorf("Got duration of %v, expecting at 0 seconds", qr.ValidDuration)
90	}
91
92	if err = handler.Close(); err != nil {
93		t.Errorf("Got error %v, expecting success", err)
94	}
95}
96
97func contains(s []string, e string) bool {
98	for _, a := range s {
99		if a == e {
100			return true
101		}
102	}
103	return false
104}
105