<commit_before>/*
// Copyright (c) 2016 Intel Corporation
//
// 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 main

import (
	"testing"

	"github.com/01org/ciao/payloads"
	"github.com/01org/ciao/testutil"
)

func TestParseAttachVolumePayload(t *testing.T) {
	instance, volume, err := parseAttachVolumePayload([]byte(testutil.AttachVolumeYaml))
	if err != nil {
		t.Fatalf("parseAttachVolumePayload failed: %v", err)
	}
	if instance != testutil.InstanceUUID || volume != testutil.VolumeUUID {
		t.Fatalf("VolumeUUID or InstanceUUID is invalid")
	}

	_, _, err = parseAttachVolumePayload([]byte("  -"))
	if err == nil || err.code != payloads.AttachVolumeInvalidPayload {
		t.Fatalf("AttachVolumeInvalidPayload error expected")
	}

	_, _, err = parseAttachVolumePayload([]byte(testutil.BadAttachVolumeYaml))
	if err == nil || err.code != payloads.AttachVolumeInvalidData {
		t.Fatalf("AttachVolumeInvalidData error expected")
	}
}

func TestParseDetachVolumePayload(t *testing.T) {
	instance, volume, err := parseDetachVolumePayload([]byte(testutil.DetachVolumeYaml))
	if err != nil {
		t.Fatalf("parseDetachVolumePayload failed: %v", err)
	}
	if instance != testutil.InstanceUUID || volume != testutil.VolumeUUID {
		t.Fatalf("VolumeUUID or InstanceUUID is invalid")
	}

	_, _, err = parseDetachVolumePayload([]byte("  -"))
	if err == nil || err.code != payloads.DetachVolumeInvalidPayload {
		t.Fatalf("AttachVolumeInvalidPayload error expected")
	}

	_, _, err = parseDetachVolumePayload([]byte(testutil.BadDetachVolumeYaml))
	if err == nil || err.code != payloads.DetachVolumeInvalidData {
		t.Fatalf("DetachVolumeInvalidData error expected")
	}
}<commit_msg>ciao-launcher: Add additional payload tests<commit_after>/*
// Copyright (c) 2016 Intel Corporation
//
// 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 main

import (
	"reflect"
	"testing"

	yaml "gopkg.in/yaml.v2"

	"github.com/01org/ciao/networking/libsnnet"
	"github.com/01org/ciao/payloads"
	"github.com/01org/ciao/testutil"
)

var startTests = []struct {
	payload string
	config  *vmConfig
}{
	{
		`
start:
  requested_resources:
     - type: vcpus
       value: 2
     - type: mem_mb
       value: 370
     - type: disk_mb
       value: 8000
  instance_uuid: d7d86208-b46c-4465-9018-ee14087d415f
  tenant_uuid: 67d86208-000-4465-9018-fe14087d415f
  fw_type: legacy
  vm_type: qemu
  networking:
    vnic_mac: 02:00:e6:f5:af:f9
    vnic_uuid: 67d86208-b46c-0000-9018-fe14087d415f
    concentrator_ip: 192.168.42.21
    concentrator_uuid: 67d86208-b46c-4465-0000-fe14087d415f
    subnet: 192.168.8.0/21
    private_ip: 192.168.8.2
  storage:
    id: 69e84267-ed01-4738-b15f-b47de06b62e7
    boot: true
`,
		&vmConfig{
			Cpus:       2,
			Mem:        370,
			Disk:       8000,
			Instance:   "d7d86208-b46c-4465-9018-ee14087d415f",
			Legacy:     true,
			VnicMAC:    "02:00:e6:f5:af:f9",
			VnicIP:     "192.168.8.2",
			ConcIP:     "192.168.42.21",
			SubnetIP:   "192.168.8.0/21",
			TenantUUID: "67d86208-000-4465-9018-fe14087d415f",
			ConcUUID:   "67d86208-b46c-4465-0000-fe14087d415f",
			VnicUUID:   "67d86208-b46c-0000-9018-fe14087d415f",
			SSHPort:    35050,
			Volumes: []volumeConfig{
				{
					"69e84267-ed01-4738-b15f-b47de06b62e7",
					true,
				},
			},
		},
	},
	{
		"start",
		nil,
	},
	{
		`
start:
  requested_resources:
     - type: vcpus
       value: 2
     - type: mem_mb
       value: 370
     - type: disk_mb
       value: 8000
  instance_uuid: imnotvalid
  tenant_uuid: 67d86208-000-4465-9018-fe14087d415f
  fw_type: legacy
  networking:
    vnic_mac: 02:00:e6:f5:af:f9
    vnic_uuid: 67d86208-b46c-0000-9018-fe14087d415f
    concentrator_ip: 192.168.42.21
    concentrator_uuid: 67d86208-b46c-4465-0000-fe14087d415f
    subnet: 192.168.8.0/21
    private_ip: 192.168.8.2
  storage:
    id: 69e84267-ed01-4738-b15f-b47de06b62e7
    boot: true
`,
		nil,
	},
	{
		`
start:
  requested_resources:
     - type: vcpus
       value: 2
     - type: mem_mb
       value: 370
     - type: disk_mb
       value: 8000
  instance_uuid: d7d86208-b46c-4465-9018-ee14087d415f
  tenant_uuid: 67d86208-000-4465-9018-fe14087d415f
  fw_type: imnotvalid
  networking:
    vnic_mac: 02:00:e6:f5:af:f9
    vnic_uuid: 67d86208-b46c-0000-9018-fe14087d415f
    concentrator_ip: 192.168.42.21
    concentrator_uuid: 67d86208-b46c-4465-0000-fe14087d415f
    subnet: 192.168.8.0/21
    private_ip: 192.168.8.2
  storage:
    id: 69e84267-ed01-4738-b15f-b47de06b62e7
    boot: true
`,
		nil,
	},
	{
		`
start:
  requested_resources:
     - type: vcpus
       value: 2
     - type: mem_mb
       value: 370
     - type: disk_mb
       value: 8000
  instance_uuid: d7d86208-b46c-4465-9018-ee14087d415f
  vm_type: askajajlsj
  tenant_uuid: 67d86208-000-4465-9018-fe14087d415f
  fw_type: legacy
  networking:
    vnic_mac: 02:00:e6:f5:af:f9
    vnic_uuid: 67d86208-b46c-0000-9018-fe14087d415f
    concentrator_ip: 192.168.42.21
    concentrator_uuid: 67d86208-b46c-4465-0000-fe14087d415f
    subnet: 192.168.8.0/21
    private_ip: 192.168.8.2
  storage:
    id: 69e84267-ed01-4738-b15f-b47de06b62e7
    boot: true
`,
		nil,
	},
}

func TestParseAttachVolumePayload(t *testing.T) {
	instance, volume, err := parseAttachVolumePayload([]byte(testutil.AttachVolumeYaml))
	if err != nil {
		t.Fatalf("parseAttachVolumePayload failed: %v", err)
	}
	if instance != testutil.InstanceUUID || volume != testutil.VolumeUUID {
		t.Fatalf("VolumeUUID or InstanceUUID is invalid")
	}

	_, _, err = parseAttachVolumePayload([]byte("  -"))
	if err == nil || err.code != payloads.AttachVolumeInvalidPayload {
		t.Fatalf("AttachVolumeInvalidPayload error expected")
	}

	_, _, err = parseAttachVolumePayload([]byte(testutil.BadAttachVolumeYaml))
	if err == nil || err.code != payloads.AttachVolumeInvalidData {
		t.Fatalf("AttachVolumeInvalidData error expected")
	}
}

func TestParseDetachVolumePayload(t *testing.T) {
	instance, volume, err := parseDetachVolumePayload([]byte(testutil.DetachVolumeYaml))
	if err != nil {
		t.Fatalf("parseDetachVolumePayload failed: %v", err)
	}
	if instance != testutil.InstanceUUID || volume != testutil.VolumeUUID {
		t.Fatalf("VolumeUUID or InstanceUUID is invalid")
	}

	_, _, err = parseDetachVolumePayload([]byte("  -"))
	if err == nil || err.code != payloads.DetachVolumeInvalidPayload {
		t.Fatalf("AttachVolumeInvalidPayload error expected")
	}

	_, _, err = parseDetachVolumePayload([]byte(testutil.BadDetachVolumeYaml))
	if err == nil || err.code != payloads.DetachVolumeInvalidData {
		t.Fatalf("DetachVolumeInvalidData error expected")
	}
}

func TestParseStartPayload(t *testing.T) {
	for i, st := range startTests {
		cfg, err := parseStartPayload([]byte(st.payload))
		if st.config == nil {
			if cfg != nil {
				t.Errorf("Expected nil config due to bad payload %d", i)
			}
			continue
		}
		if err != nil {
			t.Errorf("Failed to parse payload %d : %v", i, err)
			continue
		}
		if !reflect.DeepEqual(cfg, st.config) {
			t.Errorf("Start payload %d does not match", i)
		}
	}
}

func compareNetEvents(t *testing.T, ev *libsnnet.SsntpEventInfo, eventData *payloads.TenantAddedEvent) {
	if eventData.AgentUUID != testutil.AgentUUID ||
		eventData.AgentIP != ev.CnIP ||
		eventData.TenantUUID != ev.TenantID ||
		eventData.TenantSubnet != ev.SubnetID ||
		eventData.ConcentratorUUID != ev.ConcID ||
		eventData.ConcentratorIP != ev.CnciIP ||
		eventData.SubnetKey != ev.SubnetKey {
		t.Errorf("payloads and ssntp events do not match")
	}
}

func TestGenerateNetEventPayload(t *testing.T) {
	ev := &libsnnet.SsntpEventInfo{
		Event:     libsnnet.SsntpTunAdd,
		CnciIP:    testutil.CNCIIP,
		CnIP:      testutil.InstancePrivateIP,
		Subnet:    testutil.TenantSubnet,
		TenantID:  testutil.TenantUUID,
		SubnetID:  "",
		ConcID:    testutil.CNCIUUID,
		CnID:      testutil.AgentUUID,
		SubnetKey: 1,
	}

	pl, err := generateNetEventPayload(ev, testutil.AgentUUID)
	if err != nil {
		t.Fatalf("Failed to generate payload : %v", err)
	}
	var eventData payloads.EventTenantAdded
	err = yaml.Unmarshal(pl, &eventData)
	if err != nil {
		t.Fatalf("Unable to unmarshall event : %v", err)
	}

	compareNetEvents(t, ev, &eventData.TenantAdded)

	ev.Event = libsnnet.SsntpTunDel
	pl, err = generateNetEventPayload(ev, testutil.AgentUUID)
	if err != nil {
		t.Fatalf("Failed to generate payload : %v", err)
	}
	var eventData2 payloads.EventTenantRemoved
	err = yaml.Unmarshal(pl, &eventData2)
	if err != nil {
		t.Fatalf("Unable to unmarshall event : %v", err)
	}

	compareNetEvents(t, ev, &eventData2.TenantRemoved)

	ev.Event = 666
	_, err = generateNetEventPayload(ev, testutil.AgentUUID)
	if err == nil {
		t.Errorf("generateNetEventPayload should have failed on invalid event type")
	}
}

func TestParseRestartPayload(t *testing.T) {
	instance, err := parseRestartPayload([]byte(testutil.RestartYaml))
	if err != nil {
		t.Fatalf("Failed to parse restart payload : %v", err.err)
	}
	if instance != testutil.InstanceUUID {
		t.Errorf("Wrong instance UUID.  Expected %s found %s", instance,
			testutil.InstanceUUID)
	}
}

func TestParseDeletePayload(t *testing.T) {
	instance, err := parseDeletePayload([]byte(testutil.DeleteYaml))
	if err != nil {
		t.Fatalf("Failed to parse delete payload : %v", err.err)
	}
	if instance != testutil.InstanceUUID {
		t.Errorf("Wrong instance UUID.  Expected %s found %s", instance,
			testutil.InstanceUUID)
	}
}

func TestParseStopPayload(t *testing.T) {
	instance, err := parseStopPayload([]byte(testutil.StopYaml))
	if err != nil {
		t.Fatalf("Failed to parse stop payload : %v", err.err)
	}
	if instance != testutil.InstanceUUID {
		t.Errorf("Wrong instance UUID.  Expected %s found %s", instance,
			testutil.InstanceUUID)
	}
}
{'fraction_non_alphanumeric': 0.12401795735129069, 'fraction_numerical': 0.07856341189674523, 'mean_word_length': 3.7188879082082966, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 15, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5422943', 'n_tokens_mistral': 4560, 'n_tokens_neox': 3976, 'n_words': 827}