Debian Codegraph @

api/v0/testutil/testingutil.go

raw symbols SCIP
1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 //Collection of resources for teting mesos artifacts.
20 package testutil
21
22 import (
23 "bytes"
24 "fmt"
25 "github.com/gogo/protobuf/proto"
26 log "github.com/golang/glog"
27 "github.com/mesos/mesos-go/api/v0/upid"
28 "github.com/stretchr/testify/assert"
29 "io"
30 "net"
31 "net/http"
32 "net/http/httptest"
33 "os"
34 "reflect"
35 "sync"
36 "testing"
37 )
38
39 //MockMesosHttpProcess represents a remote http process: master or slave.
40 type MockMesosHttpServer struct {
41 PID *upid.UPID
42 Addr string
43 server *httptest.Server
44 t *testing.T
45 when map[string]http.HandlerFunc
46 lock *sync.Mutex
47 }
48
49 type When interface {
50 Do(http.HandlerFunc)
51 }
52
53 type WhenFunc func(http.HandlerFunc)
54
55 func (w WhenFunc) Do(f http.HandlerFunc) {
56 w(f)
57 }
58
59 func (m *MockMesosHttpServer) On(uri string) When {
60 log.V(2).Infof("when %v do something special", uri)
61 return WhenFunc(func(f http.HandlerFunc) {
62 log.V(2).Infof("registered callback for %v", uri)
63 m.when[uri] = f
64 })
65 }
66
67 func NewMockMasterHttpServer(t *testing.T, handler func(rsp http.ResponseWriter, req *http.Request)) *MockMesosHttpServer {
68 var server *httptest.Server
69 when := make(map[string]http.HandlerFunc)
70 stateHandler := func(rsp http.ResponseWriter, req *http.Request) {
71 if "/state" == req.RequestURI {
72 state := fmt.Sprintf(`{ "leader": "master@%v" }`, server.Listener.Addr())
73 log.V(1).Infof("returning JSON %v", state)
74 io.WriteString(rsp, state)
75 } else if f, found := when[req.RequestURI]; found {
76 f(rsp, req)
77 } else {
78 handler(rsp, req)
79 }
80 }
81 h, lock := guardedHandler(http.HandlerFunc(stateHandler))
82 server = httptest.NewServer(h)
83 assert.NotNil(t, server)
84 addr := server.Listener.Addr().String()
85 pid, err := upid.Parse("master@" + addr)
86 assert.NoError(t, err)
87 assert.NotNil(t, pid)
88 log.Infoln("Created test Master http server with PID", pid.String())
89 return &MockMesosHttpServer{PID: pid, Addr: addr, server: server, t: t, when: when, lock: lock}
90 }
91
92 func NewMockSlaveHttpServer(t *testing.T, handler func(rsp http.ResponseWriter, req *http.Request)) *MockMesosHttpServer {
93 h, lock := guardedHandler(http.HandlerFunc(handler))
94 server := httptest.NewServer(h)
95 assert.NotNil(t, server)
96 addr := server.Listener.Addr().String()
97 pid, err := upid.Parse("slave(1)@" + addr)
98 assert.NoError(t, err)
99 assert.NotNil(t, pid)
100 assert.NoError(t, os.Setenv("MESOS_SLAVE_PID", pid.String()))
101 assert.NoError(t, os.Setenv("MESOS_SLAVE_ID", "test-slave-001"))
102 log.Infoln("Created test Slave http server with PID", pid.String())
103 return &MockMesosHttpServer{PID: pid, Addr: addr, server: server, t: t, lock: lock}
104 }
105
106 // guardedHandler wraps http.Handler invocations with a mutex
107 func guardedHandler(h http.Handler) (http.Handler, *sync.Mutex) {
108 var lock sync.Mutex
109 return http.HandlerFunc(func(rsp http.ResponseWriter, req *http.Request) {
110 lock.Lock()
111 defer lock.Unlock()
112 h.ServeHTTP(rsp, req)
113 }), &lock
114 }
115
116 func (s *MockMesosHttpServer) Close() {
117 s.lock.Lock()
118 defer s.lock.Unlock()
119 s.server.Close()
120 }
121
122 //MockMesosClient Http client to communicate with mesos processes (master,sched,exec)
123 type MockMesosClient struct {
124 pid *upid.UPID
125 t *testing.T
126 }
127
128 func NewMockMesosClient(t *testing.T, pid *upid.UPID) *MockMesosClient {
129 return &MockMesosClient{t: t, pid: pid}
130 }
131
132 // sendMessage Mocks sending event messages to a processes such as master, sched or exec.
133 func (c *MockMesosClient) SendMessage(targetPid *upid.UPID, message proto.Message) {
134 if c.t == nil {
135 panic("MockMesosClient needs a testing context.")
136 }
137
138 messageName := reflect.TypeOf(message).Elem().Name()
139 data, err := proto.Marshal(message)
140 assert.NoError(c.t, err)
141 hostport := net.JoinHostPort(targetPid.Host, targetPid.Port)
142 targetURL := fmt.Sprintf("http://%s/%s/mesos.internal.%s", hostport, targetPid.ID, messageName)
143 log.Infoln("MockMesosClient Sending message to", targetURL)
144 req, err := http.NewRequest("POST", targetURL, bytes.NewReader(data))
145 assert.NoError(c.t, err)
146 req.Header.Add("Libprocess-From", c.pid.String())
147 req.Header.Add("Content-Type", "application/x-protobuf")
148 resp, err := http.DefaultClient.Do(req)
149 assert.NoError(c.t, err)
150 assert.Equal(c.t, http.StatusAccepted, resp.StatusCode)
151 }