2022-05-19 20:13:51 +00:00
|
|
|
package edgestacks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
portainer "github.com/portainer/portainer/api"
|
|
|
|
"github.com/portainer/portainer/api/apikey"
|
|
|
|
"github.com/portainer/portainer/api/dataservices"
|
|
|
|
"github.com/portainer/portainer/api/datastore"
|
|
|
|
"github.com/portainer/portainer/api/filesystem"
|
|
|
|
"github.com/portainer/portainer/api/http/security"
|
2022-12-01 06:40:52 +00:00
|
|
|
"github.com/portainer/portainer/api/internal/edge/edgestacks"
|
2023-04-03 06:19:17 +00:00
|
|
|
"github.com/portainer/portainer/api/internal/testhelpers"
|
2022-05-19 20:13:51 +00:00
|
|
|
"github.com/portainer/portainer/api/jwt"
|
2022-09-28 17:56:32 +00:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2022-05-19 20:13:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Helpers
|
2023-05-30 14:02:22 +00:00
|
|
|
func setupHandler(t *testing.T) (*Handler, string) {
|
2022-05-19 20:13:51 +00:00
|
|
|
t.Helper()
|
|
|
|
|
2023-05-30 14:02:22 +00:00
|
|
|
_, store := datastore.MustNewTestStore(t, true, true)
|
2022-05-19 20:13:51 +00:00
|
|
|
|
|
|
|
jwtService, err := jwt.NewService("1h", store)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
user := &portainer.User{ID: 2, Username: "admin", Role: portainer.AdministratorRole}
|
|
|
|
err = store.User().Create(user)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
apiKeyService := apikey.NewAPIKeyService(store.APIKeyRepository(), store.User())
|
|
|
|
rawAPIKey, _, err := apiKeyService.GenerateApiKey(*user, "test")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2022-12-01 06:40:52 +00:00
|
|
|
edgeStacksService := edgestacks.NewService(store)
|
|
|
|
|
2022-05-19 20:13:51 +00:00
|
|
|
handler := NewHandler(
|
|
|
|
security.NewRequestBouncer(store, jwtService, apiKeyService),
|
|
|
|
store,
|
2022-12-01 06:40:52 +00:00
|
|
|
edgeStacksService,
|
2022-05-19 20:13:51 +00:00
|
|
|
)
|
|
|
|
|
2022-09-14 05:59:47 +00:00
|
|
|
tmpDir := t.TempDir()
|
2022-05-19 20:13:51 +00:00
|
|
|
|
|
|
|
fs, err := filesystem.NewService(tmpDir, "")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
handler.FileService = fs
|
|
|
|
|
|
|
|
settings, err := handler.DataStore.Settings().Settings()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
settings.EnableEdgeComputeFeatures = true
|
|
|
|
|
|
|
|
err = handler.DataStore.Settings().UpdateSettings(settings)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2023-04-03 06:19:17 +00:00
|
|
|
handler.GitService = testhelpers.NewGitService(errors.New("Clone error"), "git-service-id")
|
2022-05-19 20:13:51 +00:00
|
|
|
|
2023-05-30 14:02:22 +00:00
|
|
|
return handler, rawAPIKey
|
2022-05-19 20:13:51 +00:00
|
|
|
}
|
|
|
|
|
2022-12-14 13:41:45 +00:00
|
|
|
func createEndpointWithId(t *testing.T, store dataservices.DataStore, endpointID portainer.EndpointID) portainer.Endpoint {
|
2022-05-19 20:13:51 +00:00
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
endpoint := portainer.Endpoint{
|
|
|
|
ID: endpointID,
|
|
|
|
Name: "test-endpoint-" + strconv.Itoa(int(endpointID)),
|
|
|
|
Type: portainer.EdgeAgentOnDockerEnvironment,
|
|
|
|
URL: "https://portainer.io:9443",
|
|
|
|
EdgeID: "edge-id",
|
|
|
|
LastCheckInDate: time.Now().Unix(),
|
|
|
|
}
|
|
|
|
|
|
|
|
err := store.Endpoint().Create(&endpoint)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return endpoint
|
|
|
|
}
|
|
|
|
|
2022-12-14 13:41:45 +00:00
|
|
|
func createEndpoint(t *testing.T, store dataservices.DataStore) portainer.Endpoint {
|
|
|
|
return createEndpointWithId(t, store, 5)
|
|
|
|
}
|
|
|
|
|
2022-05-19 20:13:51 +00:00
|
|
|
func createEdgeStack(t *testing.T, store dataservices.DataStore, endpointID portainer.EndpointID) portainer.EdgeStack {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
edgeGroup := portainer.EdgeGroup{
|
|
|
|
ID: 1,
|
|
|
|
Name: "EdgeGroup 1",
|
|
|
|
Dynamic: false,
|
|
|
|
TagIDs: nil,
|
|
|
|
Endpoints: []portainer.EndpointID{endpointID},
|
|
|
|
PartialMatch: false,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := store.EdgeGroup().Create(&edgeGroup)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
edgeStackID := portainer.EdgeStackID(14)
|
|
|
|
edgeStack := portainer.EdgeStack{
|
|
|
|
ID: edgeStackID,
|
|
|
|
Name: "test-edge-stack-" + strconv.Itoa(int(edgeStackID)),
|
|
|
|
Status: map[portainer.EndpointID]portainer.EdgeStackStatus{
|
2022-12-21 00:18:51 +00:00
|
|
|
endpointID: {Details: portainer.EdgeStackStatusDetails{Ok: true}, Error: "", EndpointID: endpointID},
|
2022-05-19 20:13:51 +00:00
|
|
|
},
|
|
|
|
CreationDate: time.Now().Unix(),
|
|
|
|
EdgeGroups: []portainer.EdgeGroupID{edgeGroup.ID},
|
|
|
|
ProjectPath: "/project/path",
|
|
|
|
EntryPoint: "entrypoint",
|
|
|
|
Version: 237,
|
|
|
|
ManifestPath: "/manifest/path",
|
|
|
|
DeploymentType: portainer.EdgeStackDeploymentKubernetes,
|
|
|
|
}
|
|
|
|
|
|
|
|
endpointRelation := portainer.EndpointRelation{
|
|
|
|
EndpointID: endpointID,
|
|
|
|
EdgeStacks: map[portainer.EdgeStackID]bool{
|
|
|
|
edgeStack.ID: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err = store.EdgeStack().Create(edgeStack.ID, &edgeStack)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = store.EndpointRelation().Create(&endpointRelation)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return edgeStack
|
|
|
|
}
|