Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

testing: add handling of unknown providers and empty terraform #55

Merged
merged 3 commits into from
Aug 30, 2021

Conversation

xlr-8
Copy link
Contributor

@xlr-8 xlr-8 commented Aug 12, 2021

This PR adds the handling of unknown provider (eg. vmware) and also cases of empty terraform plan/module. That allows to give better output when the cost estimated is '0.0'. Now instead of having '0.0' an error will be returned.

In order to improve the testing, some pricing dump got also injected as part of it.

Related to: #21

@xlr-8 xlr-8 self-assigned this Aug 12, 2021
@xlr-8 xlr-8 force-pushed the hr-improve-error branch 2 times, most recently from d456e9d to aea48c5 Compare August 12, 2021 12:36
Copy link
Member

@xescugc xescugc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like the HCL case is not handled and that this is maybe not the best way to handle the case that we do not have 0.0.

There are 2 ways to do it:

  • On the functions of the cost.Plan
  • Initialization of the Plan if the prior and planned are totally empty.

Or following what you did you could also add the if onto the terraform.ExtractQueriesFromHCL

e2e/aws_estimation_test.go Show resolved Hide resolved
Comment on lines 7 to 8
ErrNoQueries = fmt.Errorf("no terraform entities found, looks empty")
ErrNoKnownProvider = fmt.Errorf("terraform providers are not yet supported")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's the same but I would use error.New as fmt.Errorf is mainly used to create interpolation messages with %s and so on.

terraform/hcl.go Outdated
}

// extractHCLModule returns the resources found in the provided module.
func extractHCLModule(providers map[string]Provider, parser *configs.Parser, modPath string, modName string, mod *configs.Module, evalCtx *hcl.EvalContext) ([]query.Resource, error) {
queries := make([]query.Resource, 0, len(mod.ManagedResources))

// knownProvider will remain false, until one resource from a
// known provider is encountered. This allows to know if through
// all the given resources some could be estimated or not
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it's GH but I see a weird space on could be estimated or not


// Errors that might be returned from procesing the HCL
var (
ErrNoQueries = fmt.Errorf("no terraform entities found, looks empty")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Means empty or that we do not support any of the resources on the Plan/HCL?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Means no queries were found - eg something generates a diff where there is nothing to process or contains no files.

@xlr-8
Copy link
Contributor Author

xlr-8 commented Aug 12, 2021

I feel like the HCL case is not handled and that this is maybe not the best way to handle the case that we do not have 0.0.

Why? Both cases are tested on the PR though?

There are 2 ways to do it:
* On the functions of the cost.Plan
* Initialization of the Plan if the prior and planned are totally empty.

HCL doesn't contain prior and plan costs. Plus as we are speaking of plan, you could have some prior queries, but no new ones.

Or following what you did you could also add the if onto the terraform.ExtractQueriesFromHCL

I'm not sure to follow your point there.

@xlr-8
Copy link
Contributor Author

xlr-8 commented Aug 12, 2021

I'm currently having issues with the pipeline to properly test it, so I'll remove the need review label.

@xlr-8 xlr-8 removed the need review label Aug 12, 2021
@xescugc
Copy link
Member

xescugc commented Aug 13, 2021

Why? Both cases are tested on the PR though?

IDK why I did only see the NoQuereis on the State and not the HCL case

HCL doesn't contain prior and plan costs. Plus as we are speaking of plan, you could have some prior queries, but no new ones.

What I don't like about your solution is that it's the same logic in 2 places, and the Queries itself are of no "use" until you put them into a State. And both of them use the State to calculate so I would move that logic to the State initialization, so then it's not HCL nor Plan but common place, and also it's where the actual calculations are made so it would make sense to be there.

Also as the query returns s []query you could calculate them from different files and then aggregate all the queries into 1 State which would validate if it's empty or not.

@xlr-8
Copy link
Contributor Author

xlr-8 commented Aug 13, 2021

Done, pipeline has been fixed too to work with the dump.

Copy link
Member

@xescugc xescugc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rs

@xlr-8
Copy link
Contributor Author

xlr-8 commented Aug 20, 2021

Done

estimation.go Outdated
@@ -27,7 +27,7 @@ func EstimateTerraformPlan(ctx context.Context, backend Backend, plan io.Reader,
}

priorQueries, err := tfplan.ExtractPriorQueries()
if err != nil {
if err != nil && err != terraform.ErrNoQueries {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not needed no?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, AFAIK this is needed in case there are no prior queries.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But the ExtractPriorQueries doeds not return this error anymore, this was useful before but now it's not.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, done

@xlr-8
Copy link
Contributor Author

xlr-8 commented Aug 20, 2021

I've updated the Makefile/docker-compose file to ease the testing locally/in the CI

xescugc
xescugc previously approved these changes Aug 30, 2021
Copy link
Member

@xescugc xescugc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RS + merge

Hugo Rosnet added 3 commits August 30, 2021 16:18
The testdata has been re-arranged to better reflect which providers where
being tested; this allows to deal more easily with tests and the
providers which are not yet supported.

Some SQL dump of a previous ingestion has also been added to be able to
test things locally more easily, it will be injected into the database
whenever this one is started. The database can now be access via `make
db-cli`
Instead of returning 0 as a cost estimated, Terracost will now return an
error depending on the condition: no known providers were found, so it
had no data to estimate it, or when a terraform module/files is empty
and nothing could be extracted.
The known providers are now put into a separated files, which helps with
adding new supported one to the default list.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants