Add troubleshooting tools
[ric-plt/vespamgr.git] / cmd / vesmgr / vesmgr_queryxappconfig_test.go
1 /*
2  *  Copyright (c) 2019 AT&T Intellectual Property.
3  *  Copyright (c) 2018-2019 Nokia.
4  *
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
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 package main
19
20 import (
21         "fmt"
22         "net"
23         "net/http"
24         "net/url"
25         "os"
26         "testing"
27         "time"
28
29         "github.com/stretchr/testify/suite"
30 )
31
32 type do func(w http.ResponseWriter)
33
34 type QueryXAppsConfigTestSuite struct {
35         suite.Suite
36         listener    net.Listener
37         xAppMgrFunc do
38 }
39
40 // suite setup creates the HTTP server
41 func (suite *QueryXAppsConfigTestSuite) SetupSuite() {
42         os.Unsetenv("http_proxy")
43         os.Unsetenv("HTTP_PROXY")
44         var err error
45         suite.listener, err = net.Listen("tcp", ":0")
46         suite.Nil(err)
47         go runXAppMgr(suite.listener, "/test_url/", suite)
48 }
49
50 func runXAppMgr(listener net.Listener, url string, suite *QueryXAppsConfigTestSuite) {
51
52         http.HandleFunc(url, func(w http.ResponseWriter, r *http.Request) {
53                 switch r.Method {
54                 case "GET":
55                         suite.xAppMgrFunc(w)
56                 }
57         })
58         http.Serve(listener, nil)
59 }
60
61 func (suite *QueryXAppsConfigTestSuite) TestQueryXAppsConfigFailsWithTimeout() {
62         doSleep := func(w http.ResponseWriter) {
63                 time.Sleep(time.Second * 2)
64         }
65         suite.xAppMgrFunc = doSleep
66
67         data, err := queryXAppsConfig("http://"+suite.listener.Addr().String()+"/test_url/", 1)
68         suite.Equal([]byte("{}"), data)
69         suite.NotNil(err)
70         e, ok := err.(*url.Error)
71         suite.Equal(ok, true)
72         suite.Equal(e.Timeout(), true)
73 }
74
75 func (suite *QueryXAppsConfigTestSuite) TestQueryXAppsConfigFailsWithAnErrorReply() {
76         doReplyWithErr := func(w http.ResponseWriter) {
77                 http.Error(w, "405 method not allowed", http.StatusMethodNotAllowed)
78         }
79         suite.xAppMgrFunc = doReplyWithErr
80
81         data, err := queryXAppsConfig("http://"+suite.listener.Addr().String()+"/test_url/", 1)
82         suite.Equal([]byte("{}"), data)
83         suite.NotNil(err)
84         suite.Equal("405 Method Not Allowed", err.Error())
85 }
86
87 func (suite *QueryXAppsConfigTestSuite) TestQueryXAppsConfigOk() {
88         doReply := func(w http.ResponseWriter) {
89                 fmt.Fprintf(w, "reply message")
90         }
91         suite.xAppMgrFunc = doReply
92
93         data, err := queryXAppsConfig("http://"+suite.listener.Addr().String()+"/test_url/", 1)
94         suite.NotNil(data)
95         suite.Nil(err)
96         suite.Equal(data, []byte("reply message"))
97 }
98
99 func TestQueryXAppsConfigTestSuite(t *testing.T) {
100         suite.Run(t, new(QueryXAppsConfigTestSuite))
101 }