1 package integration
2
3 import (
4 "context"
5 "net"
6 "os"
7 "testing"
8 "time"
9
10 "github.com/stretchr/testify/assert"
11 "github.com/stretchr/testify/require"
12 corev1 "k8s.io/api/core/v1"
13 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
14
15 "edge-infra.dev/pkg/sds/remoteaccess/constants"
16 "edge-infra.dev/pkg/sds/remoteaccess/wireguard/store"
17 v "edge-infra.dev/pkg/sds/remoteaccess/wireguard/vpn"
18 "edge-infra.dev/test/f2"
19 "edge-infra.dev/test/f2/x/ktest"
20
21 v1cluster "edge-infra.dev/pkg/edge/apis/cluster/v1alpha1"
22 v1vpnconfig "edge-infra.dev/pkg/sds/remoteaccess/k8s/apis/vpnconfigs/v1"
23 )
24
25 var f f2.Framework
26
27 var (
28 projectID = "ret-edge-b79we3ikmc7j9mihuwst2"
29 testIPAddress = "172.16.16.12"
30
31 clusterAName = "cluster-a"
32 testClusterA = createCluster(clusterAName, "us-east1-c")
33 testVPNConfig = createVPNConfig(clusterAName, testIPAddress, true)
34 vpnNamespace = &corev1.Namespace{
35 ObjectMeta: metav1.ObjectMeta{
36 Name: constants.VPNNamespace,
37 },
38 }
39 )
40
41 func TestMain(m *testing.M) {
42 f = f2.New(context.Background(),
43 f2.WithExtensions(
44 ktest.New(),
45 )).
46 Setup(func(ctx f2.Context) (f2.Context, error) {
47 k, err := ktest.FromContext(ctx)
48 if err != nil {
49 return ctx, err
50 }
51
52 if !*k.Env.UseExistingCluster {
53 k.Timeout = 5 * time.Second
54 k.Tick = 10 * time.Millisecond
55 }
56 return ctx, nil
57 }).Teardown()
58 os.Exit(f.Run(m))
59 }
60
61 func TestUpdateStore(t *testing.T) {
62 var (
63 k *ktest.K8s
64 vpn *v.VPN
65 err error
66 )
67 feature := f2.NewFeature("UpdateStore").
68 Setup("create VPN", func(ctx f2.Context, t *testing.T) f2.Context {
69 k = ktest.FromContextT(ctx, t)
70 require.NoError(t, k.Client.Create(ctx, vpnNamespace))
71 vpn, err = v.New()
72 require.NoError(t, err)
73 require.Equal(t, map[string]*store.Store{}, vpn.Stores())
74 return ctx
75 }).
76 Test("add new store", func(ctx f2.Context, t *testing.T) f2.Context {
77 assert.NoError(t, vpn.UpdateStore(ctx, k.Client, testVPNConfig, testClusterA))
78 assert.Equal(t, 1, len(vpn.Stores()))
79 assert.Equal(t, true, vpn.Stores()[clusterAName].IsEnabled)
80 assert.Equal(t, net.ParseIP(testIPAddress), vpn.Stores()[clusterAName].GetIPAddress())
81 return ctx
82 }).
83 Test("update existing store with no changes", func(ctx f2.Context, t *testing.T) f2.Context {
84 assert.NoError(t, vpn.UpdateStore(ctx, k.Client, testVPNConfig, testClusterA))
85 assert.Equal(t, 1, len(vpn.Stores()))
86 assert.Equal(t, true, vpn.Stores()[clusterAName].IsEnabled)
87 assert.Equal(t, net.ParseIP(testIPAddress), vpn.Stores()[clusterAName].GetIPAddress())
88 return ctx
89 }).
90 Test("update changes on existing store", func(ctx f2.Context, t *testing.T) f2.Context {
91 testVPNConfig.Spec.Enabled = false
92 testVPNConfig.Status.IP = "1.2.3.4"
93 assert.NoError(t, vpn.UpdateStore(ctx, k.Client, testVPNConfig, testClusterA))
94 assert.Equal(t, 1, len(vpn.Stores()))
95 assert.Equal(t, false, vpn.Stores()[clusterAName].IsEnabled)
96 assert.Equal(t, net.ParseIP("1.2.3.4"), vpn.Stores()[clusterAName].GetIPAddress())
97 return ctx
98 }).Feature()
99 f.Test(t, feature)
100 }
101
102 func TestRemoveStore(t *testing.T) {
103 var (
104 k *ktest.K8s
105 vpn *v.VPN
106 err error
107 )
108 feature := f2.NewFeature("RemoveStore").
109 Setup("create VPN and store", func(ctx f2.Context, t *testing.T) f2.Context {
110 k = ktest.FromContextT(ctx, t)
111 vpn, err = v.New()
112 require.NoError(t, err)
113 require.NoError(t, vpn.UpdateStore(ctx, k.Client, testVPNConfig, testClusterA))
114 return ctx
115 }).
116 Test("do nothing when store does not exist", func(ctx f2.Context, t *testing.T) f2.Context {
117 vpn.RemoveStore("not a store")
118 assert.Equal(t, 1, len(vpn.Stores()))
119 return ctx
120 }).
121 Test("store removed successfully", func(ctx f2.Context, t *testing.T) f2.Context {
122 vpn.RemoveStore(clusterAName)
123 assert.Equal(t, 0, len(vpn.Stores()))
124 return ctx
125 }).
126 Feature()
127 f.Test(t, feature)
128 }
129
130 func createCluster(name, location string) *v1cluster.Cluster {
131 return &v1cluster.Cluster{
132 ObjectMeta: metav1.ObjectMeta{Name: name},
133 Spec: v1cluster.ClusterSpec{
134 Banner: "dev0-zynstra",
135 Fleet: "store",
136 Location: location,
137 Name: "4c4d-30-05-22",
138 Organization: "edge-dev0-retail-gmi062",
139 ProjectID: projectID,
140 Type: "sds",
141 },
142 }
143 }
144
145 func createVPNConfig(name, ip string, enabled bool) *v1vpnconfig.VPNConfig {
146 return &v1vpnconfig.VPNConfig{
147 TypeMeta: metav1.TypeMeta{Kind: "VPNConfig", APIVersion: "remoteaccess.edge.ncr.com"},
148 ObjectMeta: metav1.ObjectMeta{Namespace: constants.VPNNamespace, Name: name, UID: "1234"},
149 Spec: v1vpnconfig.VPNConfigSpec{
150 Enabled: enabled,
151 },
152 Status: &v1vpnconfig.VPNConfigStatus{
153 IP: ip,
154 },
155 }
156 }
157
View as plain text