Add troubleshooting tools
[ric-plt/vespamgr.git] / cmd / vesmgr / vesmgr_queryxappssttus_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         "github.com/stretchr/testify/suite"
23         "net"
24         "net/http"
25         "net/url"
26         "os"
27         "testing"
28         "time"
29 )
30
31 type do func(w http.ResponseWriter)
32
33 type QueryXAppsStatusTestSuite struct {
34         suite.Suite
35         listener    net.Listener
36         xAppMgrFunc do
37 }
38
39 // suite setup creates the HTTP server
40 func (suite *QueryXAppsStatusTestSuite) SetupSuite() {
41         os.Unsetenv("http_proxy")
42         os.Unsetenv("HTTP_PROXY")
43         var err error
44         suite.listener, err = net.Listen("tcp", ":0")
45         suite.Nil(err)
46         go runXAppMgr(suite.listener, "/test_url/", suite)
47 }
48
49 func runXAppMgr(listener net.Listener, url string, suite *QueryXAppsStatusTestSuite) {
50
51         http.HandleFunc(url, func(w http.ResponseWriter, r *http.Request) {
52                 switch r.Method {
53                 case "GET":
54                         suite.xAppMgrFunc(w)
55                 }
56         })
57         http.Serve(listener, nil)
58 }
59
60 func (suite *QueryXAppsStatusTestSuite) TestQueryXAppsStatusFailsWithTimeout() {
61         do_sleep := func(w http.ResponseWriter) {
62                 time.Sleep(time.Second * 2)
63         }
64         suite.xAppMgrFunc = do_sleep
65
66         data, err := queryXAppsStatus("http://"+suite.listener.Addr().String()+"/test_url/", 1)
67         suite.Nil(data)
68         suite.NotNil(err)
69         e, ok := err.(*url.Error)
70         suite.Equal(ok, true)
71         suite.Equal(e.Timeout(), true)
72 }
73
74 func (suite *QueryXAppsStatusTestSuite) TestQueryXAppsStatusFailsWithAnErrorReply() {
75         do_reply_with_err := func(w http.ResponseWriter) {
76                 http.Error(w, "405 method not allowed", http.StatusMethodNotAllowed)
77         }
78         suite.xAppMgrFunc = do_reply_with_err
79
80         data, err := queryXAppsStatus("http://"+suite.listener.Addr().String()+"/test_url/", 1)
81         suite.Nil(data)
82         suite.NotNil(err)
83         suite.Equal("405 Method Not Allowed", err.Error())
84 }
85
86 func (suite *QueryXAppsStatusTestSuite) TestQueryXAppsStatusOk() {
87         do_reply := func(w http.ResponseWriter) {
88                 fmt.Fprintf(w, "reply message")
89         }
90         suite.xAppMgrFunc = do_reply
91
92         data, err := queryXAppsStatus("http://"+suite.listener.Addr().String()+"/test_url/", 1)
93         suite.NotNil(data)
94         suite.Nil(err)
95         suite.Equal(data, []byte("reply message"))
96 }
97
98 func TestQueryXAppsStatusTestSuite(t *testing.T) {
99         suite.Run(t, new(QueryXAppsStatusTestSuite))
100 }