// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

package opennebula_test

import (
	_ "embed"
	"testing"

	"github.com/cosi-project/runtime/pkg/state"
	"github.com/cosi-project/runtime/pkg/state/impl/inmem"
	"github.com/cosi-project/runtime/pkg/state/impl/namespaced"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"go.yaml.in/yaml/v4"

	"github.com/siderolabs/talos/internal/app/machined/pkg/runtime/v1alpha1/platform/opennebula"
)

//go:embed testdata/metadata.yaml
var oneContextPlain []byte

//go:embed testdata/expected.yaml
var expectedNetworkConfig string

//go:embed testdata/metadata_no_network_flag.yaml
var oneContextPlainNoNetworkFlag []byte

//go:embed testdata/expected_no_network_flag.yaml
var expectedNetworkConfigNoNetworkFlag string

func TestParseMetadata(t *testing.T) {
	o := &opennebula.OpenNebula{}
	st := state.WrapCore(namespaced.NewState(inmem.Build))

	networkConfig, err := o.ParseMetadata(st, oneContextPlain)
	require.NoError(t, err)

	marshaled, err := yaml.Marshal(networkConfig)
	require.NoError(t, err)

	t.Log(string(marshaled))
	assert.Equal(t, expectedNetworkConfig, string(marshaled))
}

// TestParseMetadataNoNetworkFlag verifies that ETH*_ variables are processed
// regardless of the NETWORK context variable value. NETWORK=YES is a
// server-side OpenNebula directive and should not gate guest-side processing.
func TestParseMetadataNoNetworkFlag(t *testing.T) {
	o := &opennebula.OpenNebula{}
	st := state.WrapCore(namespaced.NewState(inmem.Build))

	networkConfig, err := o.ParseMetadata(st, oneContextPlainNoNetworkFlag)
	require.NoError(t, err)

	marshaled, err := yaml.Marshal(networkConfig)
	require.NoError(t, err)

	t.Log(string(marshaled))
	assert.Equal(t, expectedNetworkConfigNoNetworkFlag, string(marshaled))
}
