/*
Copyright 2020 the Velero contributors.

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 version

import (
	"bytes"
	"fmt"
	"testing"

	"github.com/pkg/errors"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/mock"
	kbclient "sigs.k8s.io/controller-runtime/pkg/client"

	velerov1 "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
	"github.com/vmware-tanzu/velero/pkg/builder"
	"github.com/vmware-tanzu/velero/pkg/buildinfo"
	velerotest "github.com/vmware-tanzu/velero/pkg/test"
)

func TestPrintVersion(t *testing.T) {
	// set up some non-empty buildinfo values, but put them back to their
	// defaults at the end of the test
	var (
		origVersion      = buildinfo.Version
		origGitSHA       = buildinfo.GitSHA
		origGitTreeState = buildinfo.GitTreeState
	)
	defer func() {
		buildinfo.Version = origVersion
		buildinfo.GitSHA = origGitSHA
		buildinfo.GitTreeState = origGitTreeState
	}()
	buildinfo.Version = "v1.0.0"
	buildinfo.GitSHA = "somegitsha"
	buildinfo.GitTreeState = "dirty"

	clientVersion := fmt.Sprintf("Client:\n\tVersion: %s\n\tGit commit: %s\n", buildinfo.Version, buildinfo.FormattedGitSHA())

	tests := []struct {
		name                string
		clientOnly          bool
		serverStatusRequest *velerov1.ServerStatusRequest
		getterError         error
		want                string
	}{
		{
			name:       "client-only",
			clientOnly: true,
			want:       clientVersion,
		},
		{
			name:                "server status getter error",
			clientOnly:          false,
			serverStatusRequest: nil,
			getterError:         errors.New("an error"),
			want:                clientVersion + "<error getting server version: an error>\n",
		},
		{
			name:                "server status getter returns normally",
			clientOnly:          false,
			serverStatusRequest: builder.ForServerStatusRequest("velero", "ssr-1", "0").ServerVersion("v1.0.1").Result(),
			getterError:         nil,
			want:                clientVersion + "Server:\n\tVersion: v1.0.1\n",
		},
	}

	for _, tc := range tests {
		t.Run(tc.name, func(t *testing.T) {
			var (
				kbClient           = velerotest.NewFakeControllerRuntimeClient(t)
				serverStatusGetter = new(mockServerStatusGetter)
				buf                = new(bytes.Buffer)
			)
			defer serverStatusGetter.AssertExpectations(t)

			// GetServerStatus should only be called when clientOnly = false
			if !tc.clientOnly {
				serverStatusGetter.On("GetServerStatus", kbClient).Return(tc.serverStatusRequest, tc.getterError)
			}

			printVersion(buf, tc.clientOnly, kbClient, serverStatusGetter)

			assert.Equal(t, tc.want, buf.String())
		})
	}
}

// mockServerStatusGetter is an autogenerated mock type for the serverStatusGetter type
// Code generated by mockery v2.2.1.
type mockServerStatusGetter struct {
	mock.Mock
}

// GetServerStatus provides a mock function with given fields: mgr
func (_m *mockServerStatusGetter) GetServerStatus(kbClient kbclient.Client) (*velerov1.ServerStatusRequest, error) {
	ret := _m.Called(kbClient)

	var r0 *velerov1.ServerStatusRequest
	if rf, ok := ret.Get(0).(func(kbclient.Client) *velerov1.ServerStatusRequest); ok {
		r0 = rf(kbClient)
	} else {
		if ret.Get(0) != nil {
			r0 = ret.Get(0).(*velerov1.ServerStatusRequest)
		}
	}

	var r1 error
	if rf, ok := ret.Get(1).(func(kbclient.Client) error); ok {
		r1 = rf(kbClient)
	} else {
		r1 = ret.Error(1)
	}

	return r0, r1
}
