/*
   Copyright The containerd Authors.

   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 testutil

import (
	_ "embed"
	"fmt"
	"sync"

	"go.yaml.in/yaml/v3"
)

//go:embed images.yaml
var rawImagesList string

var testImagesOnce sync.Once

type TestImage struct {
	Ref      string   `yaml:"ref"`
	Tag      string   `yaml:"tag,omitempty"`
	Digest   string   `yaml:"digest,omitempty"`
	Variants []string `yaml:"variants,omitempty"`
}

var testImages map[string]TestImage

func getImage(key string) string {
	testImagesOnce.Do(func() {
		if err := yaml.Unmarshal([]byte(rawImagesList), &testImages); err != nil {
			fmt.Printf("Error unmarshaling test images YAML file: %v\n", err)
			panic("testing is broken")
		}
	})

	var im TestImage
	var ok bool

	if im, ok = testImages[key]; !ok {
		fmt.Printf("Image %s was not found in images list\n", key)
		panic("testing is broken")
	}

	return im.Ref + ":" + im.Tag
}
