<commit_before>/*

   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 client_tests

import (
	"bytes"
	"encoding/json"
	"errors"
	"flag"
	"fmt"
	"io/ioutil"
	"net/http"
	"os"
	"testing"
	"time"

	log "github.com/apache/incubator-trafficcontrol/lib/go-log"
	tc "github.com/apache/incubator-trafficcontrol/lib/go-tc"
	"github.com/apache/incubator-trafficcontrol/traffic_ops/client"
	_ "github.com/lib/pq"
)

//TODO: drichardson - put these in the config
var (
	to *client.Session
)

func TestMain(m *testing.M) {

	configFileName := flag.String("cfg", "", "The config file path")
	flag.Parse()

	var cfg Config
	var err error
	fmt.Printf("configFileName ---> %v\n", configFileName)
	if cfg, err = LoadConfig(*configFileName); err != nil {
		fmt.Printf("Error Loading Config %v %v\n", cfg, err)
	}

	if err = log.InitCfg(cfg); err != nil {
		fmt.Printf("Error initializing loggers: %v\n", err)
		return
	}
	log.Debugln("cfg ---> %v\n", cfg)

	log.Infof(`Using Config values:
			   TO URL:               %s
			   Db Server:            %s
			   Db User:              %s
			   Db Name:              %s
			   Db Ssl:               %t`, cfg.TOURL, cfg.DB.Hostname, cfg.DB.User, cfg.DB.DBName, cfg.DB.SSL)

	//log.Debugln("Setting up Data")
	prepareDatabase(&cfg)

	setupSession(cfg, cfg.TOURL, cfg.TOUser, cfg.TOUserPassword)

}

func getConfigOptionsFromEnv() {
}

func setupSession(cfg Config, toURL string, toUser string, toPass string) {
	var loginErr error
	toReqTimeout := time.Second * time.Duration(30)
	to, _, loginErr = client.LoginWithAgent(toURL, toUser, toPass, true, "traffic-ops-client-integration-tests", true, toReqTimeout)
	if loginErr != nil {
		fmt.Printf("\nError logging in to %v: %v\nMake sure toURL, toUser, and toPass flags are included and correct.\nExample:  go test -toUser=admin -toPass=<PASSWORD> -toURL=http://localhost:3000\n\n", toURL, loginErr)
		os.Exit(1)
	}
	log.Debugln("%v-->", toURL)

	testData, err := ioutil.ReadFile("./sample_cdn.json")
	if err != nil {
		fmt.Println(err.Error())
		os.Exit(1)
	}
	var tc TrafficControl
	err = json.Unmarshal(testData, &tc)
	if err != nil {
		log.Errorf("Cannot unmarshal the json ", err)
	}
	fmt.Printf("cdns ---> %v\n", tc.CDNs)

	//defer db.Close()
	//rows, err := db.Query("SELECT id, name FROM cdn")
	//if err != nil {
	//log.Errorf("selecting tables: %v\n", err)
	//return
	//}

	//for rows.Next() {
	//var id int
	//var name string
	//err = rows.Scan(&id, &name)
	//fmt.Println("id | name")
	//fmt.Printf("%v %s\n", id, name)
	//}
}

//GetCDN returns a Cdn struct
func GetCDN() (tc.CDN, error) {
	cdns, err := to.CDNs()
	if err != nil {
		return *new(tc.CDN), err
	}
	cdn := cdns[0]
	if cdn.Name == "ALL" {
		cdn = cdns[1]
	}
	return cdn, nil
}

//GetProfile returns a Profile Struct
func GetProfile() (tc.Profile, error) {
	profiles, err := to.Profiles()
	if err != nil {
		return *new(tc.Profile), err
	}
	return profiles[0], nil
}<commit_msg>success with logging into TO using the client and custom configuration<commit_after>/*

   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 client_tests

import (
	"bytes"
	"encoding/json"
	"errors"
	"flag"
	"fmt"
	"io/ioutil"
	"net"
	"net/http"
	"os"
	"testing"
	"time"

	log "github.com/apache/incubator-trafficcontrol/lib/go-log"
	tc "github.com/apache/incubator-trafficcontrol/lib/go-tc"
	"github.com/apache/incubator-trafficcontrol/traffic_ops/client"
	_ "github.com/lib/pq"
)

//TODO: drichardson - put these in the config
var (
	to *client.Session
)

func TestMain(m *testing.M) {

	configFileName := flag.String("cfg", "", "The config file path")
	flag.Parse()

	var cfg Config
	var err error
	fmt.Printf("configFileName ---> %v\n", configFileName)
	if cfg, err = LoadConfig(*configFileName); err != nil {
		fmt.Printf("Error Loading Config %v %v\n", cfg, err)
	}

	if err = log.InitCfg(cfg); err != nil {
		fmt.Printf("Error initializing loggers: %v\n", err)
		return
	}
	log.Debugln("cfg ---> %v\n", cfg)

	log.Infof(`Using Config values:
			   TO URL:               %s
			   Db Server:            %s
			   Db User:              %s
			   Db Name:              %s
			   Db Ssl:               %t`, cfg.TOURL, cfg.DB.Hostname, cfg.DB.User, cfg.DB.DBName, cfg.DB.SSL)

	//log.Debugln("Setting up Data")
	prepareDatabase(&cfg)

	TOSession, netAddr, err := setupSession(cfg, cfg.TOURL, cfg.TOUser, cfg.TOUserPassword)
	fmt.Printf("TOSession ---> %v\n", TOSession)
	fmt.Printf("netAddr ---> %v\n", netAddr)
	if err != nil {
		fmt.Printf("\nError logging in to %v: %v\nMake sure toURL, toUser, and toPass flags are included and correct.\nExample:  go test -toUser=%s -toURL=http://localhost:3000\n\n", cfg.TOURL, cfg.TOUserPassword, err)
		os.Exit(1)
	}

}

func getConfigOptionsFromEnv() {
}

func setupSession(cfg Config, toURL string, toUser string, toPass string) (*client.Session, net.Addr, error) {
	var err error
	var TOSession *client.Session
	var netAddr net.Addr
	toReqTimeout := time.Second * time.Duration(30)
	TOSession, netAddr, err = client.LoginWithAgent(toURL, toUser, toPass, true, "traffic-ops-client-integration-tests", true, toReqTimeout)
	if err != nil {
		return nil, nil, err
	}
	log.Debugln("%v-->", toURL)

	return TOSession, netAddr, err
}

func loadFixtureData() TrafficControl {

	fixtureData, err := ioutil.ReadFile("./sample_cdn.json")
	if err != nil {
		fmt.Println(err.Error())
		os.Exit(1)
	}
	var tc TrafficControl
	err = json.Unmarshal(fixtureData, &tc)
	if err != nil {
		log.Errorf("Cannot unmarshal the json ", err)
	}
	return tc
}

//GetCDN returns a Cdn struct
func GetCDN() (tc.CDN, error) {
	cdns, err := to.CDNs()
	if err != nil {
		return *new(tc.CDN), err
	}
	cdn := cdns[0]
	if cdn.Name == "ALL" {
		cdn = cdns[1]
	}
	return cdn, nil
}

//GetProfile returns a Profile Struct
func GetProfile() (tc.Profile, error) {
	profiles, err := to.Profiles()
	if err != nil {
		return *new(tc.Profile), err
	}
	return profiles[0], nil
}
{'fraction_non_alphanumeric': 0.14215402431962942, 'fraction_numerical': 0.004342790966994789, 'mean_word_length': 3.320825515947467, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3307175', 'n_tokens_mistral': 2558, 'n_tokens_neox': 2473, 'n_words': 705}