// integrated acceptance test
// TestSaveServer implement save server
//TODO: a workaround to figuring out how to bubble up public ip address information from the os to icsp after os build plan provisioning
// @[email protected] "@[email protected]" @[email protected] "@[email protected]" "@[email protected]" "@[email protected]"
func TestApplyDeploymentJobs(t *testing.T) {
	var (
		d            *ICSPTest
		c            *icsp.ICSPClient
		serialNumber string
	)
	if os.Getenv("ICSP_TEST_ACCEPTANCE") == "true" {
		log.Debug("implements acceptance test for ApplyDeploymentJobs")
		d, c = getTestDriverA()
		if c == nil {
			t.Fatalf("Failed to execute getTestDriver() ")
		}
		// get a Server
		osBuildPlans := make([]string, 1)
		osBuildPlans[0] = d.Tc.GetTestData(d.Env, "OSBuildPlan").(string)
		if os.Getenv("ONEVIEW_TEST_PROVISION") != "true" {
			serialNumber = d.Tc.GetTestData(d.Env, "SerialNumber").(string)
		} else {
			serialNumber = d.Tc.GetTestData(d.Env, "FreeICSPSerialNumber").(string)
		}
		s, err := c.GetServerBySerialNumber(serialNumber)
		assert.NoError(t, err, "GetServerBySerialNumber threw error -> %s, %+v\n", err, s)
		// set a custom attribute
		s.SetCustomAttribute("docker_user", "server", "docker")
		s.SetCustomAttribute("docker_hostname", "server", d.Tc.GetTestData(d.Env, "HostName").(string))
		// use test keys like from https://github.com/mitchellh/vagrant/tree/master/keys
		// private key from https://raw.githubusercontent.com/mitchellh/vagrant/master/keys/vagrant
		s.SetCustomAttribute("public_key", "server", "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA6NF8iallvQVp22WDkTkyrtvp9eWW6A8YVr+kz4TjGYe7gHzIw+niNltGEFHzD8+v1I2YJ6oXevct1YeS0o9HZyN1Q9qgCgzUFtdOKLv6IedplqoPkcmF0aYet2PkEDo3MlTBckFXPITAMzF8dJSIFo9D8HfdOV0IAdx4O7PtixWKn5y2hMNG0zQPyUecp4pzC6kivAIhyfHilFR61RGL+GPXQ2MWZWFYbAGjyiYJnAmCP3NOTd0jMZEnDkbUvxhMmBYSdETk1rRgm+R4LOzFUGaHqHDLKLX+FIPKcF96hrucXzcWyLbIbEgE98OHlnVYCzRdK8jlqm8tehUc9c9WhQ==")

		// save a server
		news, err := c.SaveServer(s)
		assert.NoError(t, err, "SaveServer threw error -> %s, %+v\n", err, news)
		assert.Equal(t, s.UUID, news.UUID, "Should return a server with the same UUID")

		// verify that the server attribute was saved by getting the server again and checking the value
		_, testValue2 := s.GetValueItem("docker_user", "server")
		assert.Equal(t, "docker", testValue2.Value, "Should return the saved custom attribute")

		if os.Getenv("ONEVIEW_TEST_PROVISION") != "true" {
			log.Info("env ONEVIEW_TEST_PROVISION != ture for ApplyDeploymentJobs")
			log.Infof("Skipping OS build for : %s, %s", osBuildPlans, serialNumber)
			return
		}
		_, err = c.ApplyDeploymentJobs(osBuildPlans, nil, s)
		assert.NoError(t, err, "ApplyDeploymentJobs threw error -> %s, %+v\n", err, news)
	} else {
		var s icsp.Server
		_, c = getTestDriverU()
		log.Debug("implements unit test for ApplyDeploymentJobs")
		testPlans := make([]string, 1)
		testPlans[0] = "testbuildplan"
		_, err := c.ApplyDeploymentJobs(testPlans, nil, s)
		assert.Error(t, err, "ApplyDeploymentJobs threw error -> %s, %+v\n", err, s)
	}
}