summaryrefslogtreecommitdiffstats
path: root/msb2pilot/src/msb2pilot/pilot/msb_test.go
blob: ca5e84fcc6a302c307707337129ba2c40b1d6d12 (plain)
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
/**
 * Copyright (c) 2018 ZTE Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and the Apache License 2.0 which both accompany this distribution,
 * and are available at http://www.eclipse.org/legal/epl-v10.html
 * and http://www.apache.org/licenses/LICENSE-2.0
 *
 * Contributors:
 *     ZTE - initial Project
 */
package pilot

import (
	"msb2pilot/models"
	"testing"
)

func TestParseServiceToConfig(t *testing.T) {
	cases := []struct {
		services        []*models.MsbService
		publishServices map[string]*models.PublishService
		want            string
	}{
		{
			services:        []*models.MsbService{},
			publishServices: map[string]*models.PublishService{},
			want: `{
"apiVersion": "networking.istio.io/v1alpha3",
"kind": "VirtualService",
"metadata": {"name": "default-apigateway"},
"spec": {"hosts":["tService"],"http":[]}
}`,
		},
		{
			services: []*models.MsbService{
				&models.MsbService{
					ConsulLabels: &models.ConsulLabels{
						NameSpace: &models.NameSpace{
							NameSpace: "service1namespace",
						},
						BaseInfo: &models.BaseInfo{
							Version: "service1v1",
							Url:     "service1url",
						},
					},
					ServiceName: "service1",
				},
				&models.MsbService{
					ConsulLabels: &models.ConsulLabels{
						NameSpace: &models.NameSpace{
							NameSpace: "service2namespace",
						},
						BaseInfo: &models.BaseInfo{
							Version: "service2v2",
							Url:     "service2url",
						},
					},
					ServiceName: "service2",
				},
			},
			publishServices: map[string]*models.PublishService{
				"service1service1v1service1namespace": &models.PublishService{
					ServiceName: "service1",
					Version:     "service1v1",
					NameSpace:   "service1namespace",
					PublishUrl:  "service1publishurl",
				},
				"service2service2v2service2namespace": &models.PublishService{
					ServiceName: "service2",
					Version:     "service2v2",
					NameSpace:   "service2namespace",
					PublishUrl:  "service2publihurl",
				},
			},
			want: `{
"apiVersion": "networking.istio.io/v1alpha3",
"kind": "VirtualService",
"metadata": {"name": "default-apigateway"},
"spec": {"hosts":["tService"],"http":[{
"match":{"uri": {"prefix": "service1publishurl"}},
"rewrite": {"uri": "service1url"},
"route": [{"destination": {"host": "service1"}}]
},{
"match":{"uri": {"prefix": "service2publihurl"}},
"rewrite": {"uri": "service2url"},
"route": [{"destination": {"host": "service2"}}]
}]}
}`,
		},
	}

	for _, cas := range cases {
		got := parseServiceToConfig("tService", cas.services, cas.publishServices)
		if got != cas.want {
			t.Errorf("parseServiceToConfig() => got %s, want %s", got, cas.want)
		}
	}
}

//func TestCreateHttpRoute(t *testing.T) {
//	cases := []struct {
//		sPath, tService, tPath, want string
//	}{
//		{ // success demo
//			sPath:    "/",
//			tService: "tService",
//			tPath:    "/",
//			want: `{
//"match":{"uri": {"prefix": "/"}},
//"rewrite": {"uri": "/"},
//"route": [{"destination": {"host": "tService"}}]
//}`,
//		},
//	}

//	for _, cas := range cases {
//		got := createHttpRoute(cas.sPath, cas.tService, cas.tPath)
//		if got != cas.want {
//			t.Errorf("createHttpRoute(%s, %s, %s) => got %s, want %s", cas.sPath, cas.tService, cas.tPath, got, cas.want)
//		}
//	}
//}