<commit_before>// Copyright 2014 ALTOROS
// Licensed under the AGPLv3, see LICENSE file for details.

package gosigma

import (
	"errors"
	"flag"
	"strings"
	"testing"
)

var live = flag.String("live", "", "run live tests against CloudSigma endpoint, specify credentials in form -live=user:pass")
var uuid = flag.String("uuid", "", "uuid of server at CloudSigma to run server specific tests")
var force = flag.String("force", "n", "force start/stop live tests")
var trace = flag.String("trace", "n", "trace live test requests/responses")

func parseCredentials() (u string, p string, e error) {
	if *live == "" {
		return
	}

	parts := strings.SplitN(*live, ":", 2)
	if len(parts) != 2 || parts[0] == "" {
		e = errors.New("Invalid credentials: " + *live)
		return
	}

	u, p = parts[0], parts[1]

	return
}

func skipTest(t *testing.T, e error) {
	if e == nil {
		t.SkipNow()
	} else {
		t.Error(e)
	}
}

func TestLiveServers(t *testing.T) {
	u, p, err := parseCredentials()
	if u == "" {
		skipTest(t, err)
		return
	}

	cli, err := NewClient(DefaultRegion, u, p, nil)
	if err != nil {
		t.Error(err)
		return
	}

	if *trace != "n" {
		cli.Logger(t)
	}

	ii, err := cli.Servers(false)
	if err != nil {
		t.Error(err)
		return
	}
	t.Logf("%v", ii)
}

func TestLiveServer(t *testing.T) {
	u, p, err := parseCredentials()
	if u == "" {
		skipTest(t, err)
		return
	}

	if *uuid == "" {
		t.Skip("-uuid=<server-uuid> must be specified")
		return
	}

	cli, err := NewClient(DefaultRegion, u, p, nil)
	if err != nil {
		t.Error(err)
		return
	}

	if *trace != "n" {
		cli.Logger(t)
	}

	s, err := cli.Server(*uuid)
	if err != nil {
		t.Error(err)
		return
	}
	t.Logf("%v", s)
}

func TestLiveStartServer(t *testing.T) {
	u, p, err := parseCredentials()
	if u == "" {
		skipTest(t, err)
		return
	}

	if *uuid == "" {
		t.Skip("-uuid=<server-uuid> must be specified")
		return
	}

	cli, err := NewClient(DefaultRegion, u, p, nil)
	if err != nil {
		t.Error(err)
		return
	}

	if *trace != "n" {
		cli.Logger(t)
	}

	s, err := cli.Server(*uuid)
	if err != nil {
		t.Error(err)
		return
	}

	if s.Status() != ServerStopped && *force == "n" {
		t.Skip("wrong server status", s.Status())
		return
	}

	if err := s.Start(); err != nil {
		t.Error(err)
	}
}<commit_msg>added live test to stop server instance<commit_after>// Copyright 2014 ALTOROS
// Licensed under the AGPLv3, see LICENSE file for details.

package gosigma

import (
	"errors"
	"flag"
	"strings"
	"testing"
)

var live = flag.String("live", "", "run live tests against CloudSigma endpoint, specify credentials in form -live=user:pass")
var uuid = flag.String("uuid", "", "uuid of server at CloudSigma to run server specific tests")
var force = flag.String("force", "n", "force start/stop live tests")
var trace = flag.String("trace", "n", "trace live test requests/responses")

func parseCredentials() (u string, p string, e error) {
	if *live == "" {
		return
	}

	parts := strings.SplitN(*live, ":", 2)
	if len(parts) != 2 || parts[0] == "" {
		e = errors.New("Invalid credentials: " + *live)
		return
	}

	u, p = parts[0], parts[1]

	return
}

func skipTest(t *testing.T, e error) {
	if e == nil {
		t.SkipNow()
	} else {
		t.Error(e)
	}
}

func TestLiveServers(t *testing.T) {
	u, p, err := parseCredentials()
	if u == "" {
		skipTest(t, err)
		return
	}

	cli, err := NewClient(DefaultRegion, u, p, nil)
	if err != nil {
		t.Error(err)
		return
	}

	if *trace != "n" {
		cli.Logger(t)
	}

	ii, err := cli.Servers(false)
	if err != nil {
		t.Error(err)
		return
	}
	t.Logf("%v", ii)
}

func TestLiveServer(t *testing.T) {
	u, p, err := parseCredentials()
	if u == "" {
		skipTest(t, err)
		return
	}

	if *uuid == "" {
		t.Skip("-uuid=<server-uuid> must be specified")
		return
	}

	cli, err := NewClient(DefaultRegion, u, p, nil)
	if err != nil {
		t.Error(err)
		return
	}

	if *trace != "n" {
		cli.Logger(t)
	}

	s, err := cli.Server(*uuid)
	if err != nil {
		t.Error(err)
		return
	}
	t.Logf("%v", s)
}

func TestLiveStart(t *testing.T) {
	u, p, err := parseCredentials()
	if u == "" {
		skipTest(t, err)
		return
	}

	if *uuid == "" {
		t.Skip("-uuid=<server-uuid> must be specified")
		return
	}

	cli, err := NewClient(DefaultRegion, u, p, nil)
	if err != nil {
		t.Error(err)
		return
	}

	if *trace != "n" {
		cli.Logger(t)
	}

	s, err := cli.Server(*uuid)
	if err != nil {
		t.Error(err)
		return
	}

	if s.Status() != ServerStopped && *force == "n" {
		t.Skip("wrong server status", s.Status())
		return
	}

	if err := s.Start(); err != nil {
		t.Error(err)
	}
}

func TestLiveStop(t *testing.T) {
	u, p, err := parseCredentials()
	if u == "" {
		skipTest(t, err)
		return
	}

	if *uuid == "" {
		t.Skip("-uuid=<server-uuid> must be specified")
		return
	}

	cli, err := NewClient(DefaultRegion, u, p, nil)
	if err != nil {
		t.Error(err)
		return
	}

	if *trace != "n" {
		cli.Logger(t)
	}

	s, err := cli.Server(*uuid)
	if err != nil {
		t.Error(err)
		return
	}

	if s.Status() != ServerRunning && *force == "n" {
		t.Skip("wrong server status", s.Status())
		return
	}

	if err := s.Stop(); err != nil {
		t.Error(err)
	}
}
{'fraction_non_alphanumeric': 0.18336928809570505, 'fraction_numerical': 0.003922337713277113, 'mean_word_length': 3.1429731925264015, 'pattern_counts': {'":': 2, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '1717916', 'n_tokens_mistral': 2159, 'n_tokens_neox': 2040, 'n_words': 546}