forked from istio/istio
/
instance.go
131 lines (109 loc) · 3.21 KB
/
instance.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
121
122
123
124
125
126
127
128
129
130
131
// Copyright 2019 Istio Authors
//
// 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 native
import (
"fmt"
"io"
"strings"
"testing"
"github.com/hashicorp/go-multierror"
appEcho "istio.io/istio/pkg/test/application/echo"
"istio.io/istio/pkg/test/framework/components/echo"
"istio.io/istio/pkg/test/framework/components/echo/common"
"istio.io/istio/pkg/test/framework/components/environment/native"
"istio.io/istio/pkg/test/framework/resource"
)
var (
_ echo.Instance = &instance{}
_ io.Closer = &instance{}
)
type instance struct {
id resource.ID
config echo.Config
workload *workload
}
// New creates a new native echo instance.
func New(ctx resource.Context, cfg echo.Config) (out echo.Instance, err error) {
env := ctx.Environment().(*native.Environment)
// Fill in defaults for any missing values.
if err = common.FillInDefaults(ctx, env.Domain, &cfg); err != nil {
return nil, err
}
c := &instance{
config: cfg,
}
c.id = ctx.TrackResource(c)
// Create the workload for this configuration and assign ports.
c.workload, err = newWorkload(ctx, &c.config)
if err != nil {
return nil, err
}
return c, nil
}
func (c *instance) ID() resource.ID {
return c.id
}
func (c *instance) WaitUntilReady(outboundInstances ...echo.Instance) error {
// No need to check for inbound readiness, since inbound ports for the native echo instance
// are configured by bootstrap.
if c.workload.sidecar == nil {
// No sidecar, nothing to do.
return nil
}
return c.workload.sidecar.WaitForConfig(common.OutboundConfigAcceptFunc(outboundInstances...))
}
func (c *instance) WaitUntilReadyOrFail(t testing.TB, outboundInstances ...echo.Instance) {
if err := c.WaitUntilReady(outboundInstances...); err != nil {
t.Fatal(err)
}
}
func (c *instance) Config() echo.Config {
return c.config
}
func (c *instance) Workloads() ([]echo.Workload, error) {
return []echo.Workload{c.workload}, nil
}
func (c *instance) WorkloadsOrFail(t testing.TB) []echo.Workload {
out, err := c.Workloads()
if err != nil {
t.Fatal(err)
}
return out
}
func (c *instance) Call(opts echo.CallOptions) (appEcho.ParsedResponses, error) {
out, err := c.workload.Call(&opts)
if err != nil {
return nil, fmt.Errorf("failed calling %s->'%s://%s:%d/%s': %v",
c.Config().Service,
strings.ToLower(string(opts.Port.Protocol)),
opts.Target.Config().Service,
opts.Port.ServicePort,
opts.Path,
err)
}
return out, nil
}
func (c *instance) CallOrFail(t testing.TB, opts echo.CallOptions) appEcho.ParsedResponses {
r, err := c.Call(opts)
if err != nil {
t.Fatal(err)
}
return r
}
func (c *instance) Close() (err error) {
if c.workload != nil {
err = multierror.Append(err, c.workload.Close()).ErrorOrNil()
}
return
}