19.10.22

A first take at auto generating self-documenting tests for VMWare Tanzu

For many years ive found that product documentation for large enterprise infra software (openshfit, tanzu, ...) isnt easy to automatically verify for end users.

 I was thinking , what if all the github repos w/ shell scripts, ansible scripts and so on that are used for things 

 A first take at auto generating self-documenting tests for VMWare Tanzu...

1) write yaml and shell scripts, simple, works.

2) use go embed so that go build my/main.go takes those yamls and stores them in the binary

3) Write them all out when the program starts up (have main.go call WriteAllToLocal()). 

4) in a situation like this you could theoretically use the same internal, external, and diagnositic validation mechanisms, and even, autogenerate documentation from your tests... 

Heres an example of how to use go:embed to do this... (in this case there are cluster.yaml and management-cluster.sh files stored in the same local directory as this, runnable program). 

To "get these files" when you run this program, you simply:

- make sure main.go calls the WriteAllToLocal() method

- go build main.go

-  ./main in any directory, and youll automatically see the Yaml and Shell script artifacts there, ready to execute. 

Since this is all done from a single golang binary, it means you can add logic to that binary that customizes those commands based on user inputs (like home directorys, access credentials for vsphere / ec2, and so on).  


import (
"corgon.com/corgon/pkg/util"
_ "embed"
"k8s.io/klog/v2"
)

var (
//go:embed corgon_cluster.yaml
ClusterConfig string

//go:embed management-cluster.sh
ManagementClusterInstall string
)

// files has all the files we are going to write to disk, so that
// they can be hacked up by the user after running corgon init the first time.
func files() map[string]string {
return map[string]string{
"corgon_cluster.yaml": ClusterConfig,
"management_cluster.sh": ManagementClusterInstall,
}
}

// WriteAllToLocal bootstraps all the tanzu framework files
// to the local directory. Next time you run it, you can customize those files
// and reuse them. That way, you can locally develop tests by just hacking up
// the scripts.
func WriteAllToLocal() {
klog.Infof("Writing out %v static files to local directory.", len(files()))
for file, contents := range files() {
if util.FileExists(file) {
klog.Infof("File exists %v , not writing...", file)
} else {
klog.Infof("File not exists %v, writing...", file)
util.StringToFile(contents, file)
}
}
}

No comments:

Post a Comment