2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-06-30 05:14:26 -04:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2022-08-04 05:08:20 -04:00
|
|
|
"context"
|
2022-06-30 05:14:26 -04:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2022-07-04 06:59:43 -04:00
|
|
|
"strings"
|
2022-06-30 05:14:26 -04:00
|
|
|
"testing"
|
|
|
|
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/hack/qemu-metadata-api/virtwrapper"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/cloud/metadata"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/logger"
|
2022-06-30 05:14:26 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestListAll(t *testing.T) {
|
|
|
|
someErr := errors.New("error")
|
|
|
|
|
|
|
|
testCases := map[string]struct {
|
|
|
|
wantErr bool
|
|
|
|
connect *stubConnect
|
|
|
|
}{
|
|
|
|
"success": {
|
|
|
|
connect: &stubConnect{
|
2023-08-18 08:25:14 -04:00
|
|
|
network: newStubNetwork([]virtwrapper.NetworkDHCPLease{
|
|
|
|
{
|
|
|
|
IPaddr: "192.0.100.1",
|
|
|
|
Hostname: "control-plane-0",
|
2022-06-30 05:14:26 -04:00
|
|
|
},
|
2023-08-18 08:25:14 -04:00
|
|
|
{
|
|
|
|
IPaddr: "192.0.100.2",
|
|
|
|
Hostname: "control-plane-1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
IPaddr: "192.0.200.1",
|
|
|
|
Hostname: "worker-0",
|
|
|
|
},
|
|
|
|
}, nil),
|
2022-06-30 05:14:26 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"LookupNetworkByName error": {
|
|
|
|
connect: &stubConnect{
|
|
|
|
getNetworkErr: someErr,
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"GetDHCPLeases error": {
|
|
|
|
connect: &stubConnect{
|
|
|
|
network: stubNetwork{
|
|
|
|
getLeaseErr: someErr,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
2022-11-26 13:44:34 -05:00
|
|
|
server := New(logger.NewTest(t), "test", "initSecretHash", tc.connect)
|
2022-06-30 05:14:26 -04:00
|
|
|
|
|
|
|
res, err := server.listAll()
|
|
|
|
|
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
assert.NoError(err)
|
|
|
|
assert.Len(tc.connect.network.leases, len(res))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestListSelf(t *testing.T) {
|
|
|
|
someErr := errors.New("error")
|
|
|
|
|
|
|
|
testCases := map[string]struct {
|
|
|
|
remoteAddr string
|
|
|
|
connect *stubConnect
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
"success": {
|
|
|
|
remoteAddr: "192.0.100.1:1234",
|
|
|
|
connect: &stubConnect{
|
2023-08-18 08:25:14 -04:00
|
|
|
network: newStubNetwork([]virtwrapper.NetworkDHCPLease{
|
|
|
|
{
|
|
|
|
IPaddr: "192.0.100.1",
|
|
|
|
Hostname: "control-plane-0",
|
2022-06-30 05:14:26 -04:00
|
|
|
},
|
2023-08-18 08:25:14 -04:00
|
|
|
}, nil),
|
2022-06-30 05:14:26 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"listAll error": {
|
|
|
|
remoteAddr: "192.0.100.1:1234",
|
|
|
|
connect: &stubConnect{
|
|
|
|
getNetworkErr: someErr,
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"remoteAddr error": {
|
|
|
|
remoteAddr: "",
|
|
|
|
connect: &stubConnect{
|
2023-08-18 08:25:14 -04:00
|
|
|
network: newStubNetwork([]virtwrapper.NetworkDHCPLease{
|
|
|
|
{
|
|
|
|
IPaddr: "192.0.100.1",
|
|
|
|
Hostname: "control-plane-0",
|
2022-06-30 05:14:26 -04:00
|
|
|
},
|
2023-08-18 08:25:14 -04:00
|
|
|
}, nil),
|
2022-06-30 05:14:26 -04:00
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"peer not found": {
|
|
|
|
remoteAddr: "192.0.200.1:1234",
|
|
|
|
connect: &stubConnect{
|
2023-08-18 08:25:14 -04:00
|
|
|
network: newStubNetwork([]virtwrapper.NetworkDHCPLease{
|
|
|
|
{
|
|
|
|
IPaddr: "192.0.100.1",
|
|
|
|
Hostname: "control-plane-0",
|
2022-06-30 05:14:26 -04:00
|
|
|
},
|
2023-08-18 08:25:14 -04:00
|
|
|
}, nil),
|
2022-06-30 05:14:26 -04:00
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
2022-11-26 13:44:34 -05:00
|
|
|
server := New(logger.NewTest(t), "test", "initSecretHash", tc.connect)
|
2022-06-30 05:14:26 -04:00
|
|
|
|
2022-08-04 05:08:20 -04:00
|
|
|
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, "http://192.0.0.1/self", nil)
|
2022-06-30 05:14:26 -04:00
|
|
|
require.NoError(err)
|
|
|
|
req.RemoteAddr = tc.remoteAddr
|
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
server.listSelf(w, req)
|
|
|
|
|
|
|
|
if tc.wantErr {
|
|
|
|
assert.NotEqual(http.StatusOK, w.Code)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
assert.Equal(http.StatusOK, w.Code)
|
|
|
|
metadataRaw, err := io.ReadAll(w.Body)
|
|
|
|
require.NoError(err)
|
|
|
|
|
2022-07-05 08:14:11 -04:00
|
|
|
var metadata metadata.InstanceMetadata
|
2022-06-30 05:14:26 -04:00
|
|
|
require.NoError(json.Unmarshal(metadataRaw, &metadata))
|
|
|
|
assert.Equal(tc.connect.network.leases[0].Hostname, metadata.Name)
|
2022-11-02 07:56:16 -04:00
|
|
|
assert.Equal(tc.connect.network.leases[0].IPaddr, metadata.VPCIP)
|
2022-06-30 05:14:26 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestListPeers(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
|
|
|
remoteAddr string
|
|
|
|
connect *stubConnect
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
"success": {
|
|
|
|
remoteAddr: "192.0.100.1:1234",
|
|
|
|
connect: &stubConnect{
|
2023-08-18 08:25:14 -04:00
|
|
|
network: newStubNetwork([]virtwrapper.NetworkDHCPLease{
|
|
|
|
{
|
|
|
|
IPaddr: "192.0.100.1",
|
|
|
|
Hostname: "control-plane-0",
|
2022-06-30 05:14:26 -04:00
|
|
|
},
|
2023-08-18 08:25:14 -04:00
|
|
|
{
|
|
|
|
IPaddr: "192.0.200.1",
|
|
|
|
Hostname: "worker-0",
|
|
|
|
},
|
|
|
|
}, nil),
|
2022-06-30 05:14:26 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"listAll error": {
|
|
|
|
remoteAddr: "192.0.100.1:1234",
|
|
|
|
connect: &stubConnect{
|
|
|
|
getNetworkErr: errors.New("error"),
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
2022-11-26 13:44:34 -05:00
|
|
|
server := New(logger.NewTest(t), "test", "initSecretHash", tc.connect)
|
2022-06-30 05:14:26 -04:00
|
|
|
|
2022-08-04 05:08:20 -04:00
|
|
|
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, "http://192.0.0.1/peers", nil)
|
2022-06-30 05:14:26 -04:00
|
|
|
require.NoError(err)
|
|
|
|
req.RemoteAddr = tc.remoteAddr
|
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
server.listPeers(w, req)
|
|
|
|
|
|
|
|
if tc.wantErr {
|
|
|
|
assert.NotEqual(http.StatusOK, w.Code)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
assert.Equal(http.StatusOK, w.Code)
|
|
|
|
metadataRaw, err := io.ReadAll(w.Body)
|
|
|
|
require.NoError(err)
|
|
|
|
|
2022-07-05 08:14:11 -04:00
|
|
|
var metadata []metadata.InstanceMetadata
|
2022-06-30 05:14:26 -04:00
|
|
|
require.NoError(json.Unmarshal(metadataRaw, &metadata))
|
|
|
|
assert.Len(metadata, len(tc.connect.network.leases))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-04 06:59:43 -04:00
|
|
|
func TestPostLog(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
|
|
|
remoteAddr string
|
|
|
|
message io.Reader
|
|
|
|
method string
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
"success": {
|
|
|
|
remoteAddr: "192.0.100.1:1234",
|
|
|
|
method: http.MethodPost,
|
|
|
|
message: strings.NewReader("test message"),
|
|
|
|
},
|
|
|
|
"no body": {
|
|
|
|
remoteAddr: "192.0.100.1:1234",
|
|
|
|
method: http.MethodPost,
|
|
|
|
message: nil,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"incorrect method": {
|
|
|
|
remoteAddr: "192.0.100.1:1234",
|
|
|
|
method: http.MethodGet,
|
|
|
|
message: strings.NewReader("test message"),
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
2022-11-26 13:44:34 -05:00
|
|
|
server := New(logger.NewTest(t), "test", "initSecretHash", &stubConnect{})
|
2022-07-04 06:59:43 -04:00
|
|
|
|
2022-08-04 05:08:20 -04:00
|
|
|
req, err := http.NewRequestWithContext(context.Background(), tc.method, "http://192.0.0.1/logs", tc.message)
|
2022-07-04 06:59:43 -04:00
|
|
|
require.NoError(err)
|
|
|
|
req.RemoteAddr = tc.remoteAddr
|
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
server.postLog(w, req)
|
|
|
|
|
|
|
|
if tc.wantErr {
|
|
|
|
assert.NotEqual(http.StatusOK, w.Code)
|
|
|
|
} else {
|
|
|
|
assert.Equal(http.StatusOK, w.Code)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-26 13:44:34 -05:00
|
|
|
func TestInitSecretHash(t *testing.T) {
|
|
|
|
defaultConnect := &stubConnect{
|
2023-08-18 08:25:14 -04:00
|
|
|
network: newStubNetwork([]virtwrapper.NetworkDHCPLease{
|
|
|
|
{
|
|
|
|
IPaddr: "192.0.100.1",
|
|
|
|
Hostname: "control-plane-0",
|
2022-11-26 13:44:34 -05:00
|
|
|
},
|
2023-08-18 08:25:14 -04:00
|
|
|
}, nil),
|
2022-11-26 13:44:34 -05:00
|
|
|
}
|
|
|
|
testCases := map[string]struct {
|
|
|
|
connect *stubConnect
|
|
|
|
method string
|
|
|
|
wantHash string
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
"success": {
|
|
|
|
connect: defaultConnect,
|
|
|
|
method: http.MethodGet,
|
|
|
|
},
|
|
|
|
"wrong method": {
|
|
|
|
connect: defaultConnect,
|
|
|
|
method: http.MethodPost,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
server := New(logger.NewTest(t), "test", tc.wantHash, defaultConnect)
|
|
|
|
|
|
|
|
req, err := http.NewRequestWithContext(context.Background(), tc.method, "http://192.0.0.1/initsecrethash", nil)
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
server.initSecretHash(w, req)
|
|
|
|
|
|
|
|
if tc.wantErr {
|
|
|
|
assert.NotEqual(http.StatusOK, w.Code)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(http.StatusOK, w.Code)
|
|
|
|
assert.Equal(tc.wantHash, w.Body.String())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-30 05:14:26 -04:00
|
|
|
type stubConnect struct {
|
|
|
|
network stubNetwork
|
|
|
|
getNetworkErr error
|
|
|
|
}
|
|
|
|
|
2023-03-20 06:03:36 -04:00
|
|
|
func (c stubConnect) LookupNetworkByName(_ string) (*virtwrapper.Network, error) {
|
2022-06-30 05:14:26 -04:00
|
|
|
return &virtwrapper.Network{Net: c.network}, c.getNetworkErr
|
|
|
|
}
|