// Copyright © 2019 IBM Corporation and others.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package functest

import (
	"encoding/json"
	"path/filepath"
	"testing"

	"github.com/appsody/appsody/cmd/cmdtest"
)

var packageStackLabels = []string{
	"id",
	"tag",
}

func TestStackPackageLabels(t *testing.T) {
	sandbox, cleanup := cmdtest.TestSetupWithSandbox(t, false)
	defer cleanup()

	sandbox.ProjectDir = filepath.Join(sandbox.TestDataPath, "starter")
	_, err := cmdtest.RunAppsody(sandbox, "stack", "package")
	if err != nil {
		t.Fatal(err)
	}

	image := "dev.local/appsody/starter:latest"
	inspectOutput, inspectErr := cmdtest.RunCmdExec("docker", []string{"inspect", image}, t)
	if inspectErr != nil {
		t.Fatal(inspectErr)
	}

	var inspect []map[string]interface{}

	err = json.Unmarshal([]byte(inspectOutput), &inspect)
	if err != nil {
		t.Fatal(err)
	}

	config := inspect[0]["Config"].(map[string]interface{})
	labelsMap := config["Labels"].(map[string]interface{})

	for _, label := range packageStackLabels {
		if labelsMap[appsodyPrefixKey+label] == nil {
			t.Errorf("Could not find %s%s label in Docker image!", appsodyPrefixKey, label)
		}
	}

	for _, label := range append(openContainerLabels, "authors") {
		if labelsMap[ociPrefixKey+label] == nil {
			t.Errorf("Could not find %s%s label in Docker image!", ociPrefixKey, label)
		}
	}

	//delete the image
	deleteImage(image, "docker", t)
}
