2 * Copyright (C) 2017-2018 "IoT.bzh"
3 * Author Clément Bénier <clement.benier@iot.bzh>
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
25 "gerrit.automotivelinux.org/gerrit/src/xds/xds-server.git/lib/xsapiv1"
26 "github.com/stretchr/testify/require"
29 func TestFolders(t *testing.T) {
30 /*init: check there is no folder*/
31 Debug(t, "check there is no folder")
32 var cfgArray []xsapiv1.FolderConfig
33 require.Nil(t, HTTPCli.Get("/folders", &cfgArray))
34 require.Equal(t, len(cfgArray), 0)
36 fPrj := xsapiv1.FolderConfig{
38 ClientPath: logDir + "testproject",
39 Type: xsapiv1.TypePathMap,
40 ClientData: "clientdatatest",
41 DataPathMap: xsapiv1.PathMapConfig{
42 ServerPath: logDir + "testserverpath",
45 var cfg xsapiv1.FolderConfig
46 Debugf(t, "create folder: \n%v", fPrj)
47 require.Nil(t, HTTPCli.Post("/folders", fPrj, &cfg))
48 require.NotNil(t, cfg)
49 Debugf(t, "result folder: \n%v", cfg)
51 isCfgPrjMatch := func(fPrj xsapiv1.FolderConfig, cfg xsapiv1.FolderConfig) {
52 re := regexp.MustCompile("^[0-9a-z]+-[0-9a-z]+-[0-9a-z]+-[0-9a-z]+-[0-9a-z]+$")
53 require.True(t, re.MatchString(cfg.ID)) //ID
54 require.Equal(t, cfg.Label, fPrj.Label) //Label
55 require.Equal(t, cfg.ClientPath, fPrj.ClientPath)
56 require.Equal(t, cfg.Type, fPrj.Type)
57 require.Equal(t, cfg.Status, "Enable")
58 require.Equal(t, cfg.IsInSync, true)
59 require.Equal(t, len(cfg.DefaultSdk), 0)
60 require.Equal(t, fPrj.ClientData, cfg.ClientData)
61 require.Equal(t, fPrj.DataPathMap.ServerPath, cfg.DataPathMap.ServerPath)
63 isCfgPrjMatch(fPrj, cfg)
64 var cfg2 xsapiv1.FolderConfig
65 require.Nil(t, HTTPCli.Get("/folders/"+cfg.ID, &cfg2))
66 isCfgPrjMatch(fPrj, cfg2)
68 require.Nil(t, HTTPCli.Get("/folders", &cfgArray))
69 require.Equal(t, len(cfgArray), 1)
71 //call with the same uid create error
72 require.NotNil(t, HTTPCli.Post("/folders", cfg, &cfg))
74 /*create/delete folders*/
75 var cfgArrayBis []xsapiv1.FolderConfig
76 require.Nil(t, HTTPCli.Post("/folders", fPrj, &cfg))
77 Debugf(t, "create folder with id=%v", cfg.ID)
78 require.Nil(t, HTTPCli.Post("/folders", fPrj, &cfg))
79 Debugf(t, "create folder with id=%v", cfg.ID)
80 require.Nil(t, HTTPCli.Get("/folders", &cfgArray))
81 require.Equal(t, len(cfgArray), 3)
82 require.Nil(t, HTTPCli.Delete("/folders/"+cfgArray[1].ID, &cfg))
83 Debugf(t, "delete folder with id=%v", cfg.ID)
84 require.Equal(t, cfg, cfgArray[1])
85 require.Nil(t, HTTPCli.Get("/folders", &cfgArrayBis))
86 require.Equal(t, len(cfgArrayBis), 2)
87 require.Nil(t, HTTPCli.Delete("/folders/"+cfgArray[0].ID, &cfg))
88 Debugf(t, "delete folder with id=%v", cfg.ID)
89 require.Equal(t, cfg, cfgArray[0])
90 require.Nil(t, HTTPCli.Get("/folders", &cfgArrayBis))
91 require.Equal(t, len(cfgArrayBis), 1)
92 require.Nil(t, HTTPCli.Delete("/folders/"+cfgArray[2].ID, &cfg))
93 Debugf(t, "delete folder with id=%v", cfg.ID)
94 require.Equal(t, cfg, cfgArray[2])
95 require.Nil(t, HTTPCli.Get("/folders", &cfgArrayBis))
96 require.Equal(t, len(cfgArrayBis), 0)
99 func TestFoldersEmptyValues(t *testing.T) {
100 fPrj := xsapiv1.FolderConfig{
101 Label: "testproject",
102 ClientPath: logDir + "testproject",
103 Type: xsapiv1.TypePathMap,
104 ClientData: "clientdatatest",
105 DataPathMap: xsapiv1.PathMapConfig{
109 var cfg xsapiv1.FolderConfig
110 /*ServerPath is empty*/
111 require.NotNil(t, HTTPCli.Post("/folders", fPrj, &cfg))
112 Debugf(t, "error while creating folder with empty serverpath \n%v", fPrj)
114 fPrj.DataPathMap.ServerPath = logDir + "sameserverpath"
116 /*ClientPath is Empty*/
117 require.NotNil(t, HTTPCli.Post("/folders", fPrj, &cfg))
118 Debugf(t, "error while creating folder with empty clientpath \n%v", fPrj)
120 fPrj.ClientPath = "logDir"
123 require.NotNil(t, HTTPCli.Post("/folders", fPrj, &cfg))
124 Debugf(t, "error while creating folder with empty type \n%v", fPrj)
126 var cfgArray []xsapiv1.FolderConfig
127 require.Nil(t, HTTPCli.Get("/folders", &cfgArray))
128 require.Equal(t, len(cfgArray), 0)
131 func TestFoldersPathMapConfig(t *testing.T) {
132 fPrj := xsapiv1.FolderConfig{
133 Label: "testproject",
134 ClientPath: logDir + "clientpathtest",
135 Type: xsapiv1.TypePathMap,
136 ClientData: "clientdatatest",
137 DataPathMap: xsapiv1.PathMapConfig{
138 ServerPath: logDir + "serverpath",
139 CheckFile: "checkfile",
142 var cfg xsapiv1.FolderConfig
144 require.NotNil(t, HTTPCli.Post("/folders", fPrj, &cfg))
145 Debugf(t, "error while creating folder with no checkfile \n%v", fPrj)
147 var checkFileClient = fPrj.ClientPath + "/checkfile"
148 var checkFileServer = fPrj.DataPathMap.ServerPath + "/checkfile"
151 os.MkdirAll(fPrj.ClientPath, 0755)
152 fPrj.DataPathMap.CheckFile = checkFileClient
153 fPrj.DataPathMap.CheckContent = "CheckContent From Client\n"
154 file, err := os.OpenFile(checkFileClient, os.O_CREATE|os.O_RDWR, 0644)
158 if err := os.Symlink(checkFileClient, checkFileServer); err != nil {
161 /*file content differ*/
162 require.NotNil(t, HTTPCli.Post("/folders", fPrj, &cfg))
163 Debugf(t, "error while creating folder with different checkfiles \n%v", fPrj)
165 /*write same message*/
166 if _, err := file.WriteString(fPrj.DataPathMap.CheckContent); err != nil {
169 require.Nil(t, HTTPCli.Post("/folders", fPrj, &cfg))
170 Debugf(t, "create folder with same checkfiles \n%v", fPrj)
172 /*check server msg: ServerUID needed*/
173 Debugf(t, "check server msg")
174 var APIcfg xsapiv1.APIConfig
175 require.Nil(t, HTTPCli.Get("/config", &APIcfg))
176 msg := "Pathmap checked message written by xds-server ID: " + APIcfg.ServerUID + "\n"
177 data, err := ioutil.ReadAll(file)
181 require.Equal(t, msg, string(data))
183 require.Nil(t, HTTPCli.Delete("/folders/"+cfg.ID, &cfg))
184 var cfgArray []xsapiv1.FolderConfig
185 require.Nil(t, HTTPCli.Get("/folders", &cfgArray))
186 require.Equal(t, len(cfgArray), 0)