Skip to content

Cache API tests & unifying behavior - #61229

Merged
Ch3LL merged 32 commits into
saltstack:masterfrom
waynew:tc/cache_tests
Apr 8, 2022
Merged

Ch3LL merged 32 commits into
saltstack:masterfrom
waynew:tc/cache_tests

Conversation

@waynew

@waynew waynew commented Nov 11, 2021 •

Copy link
Copy Markdown
Contributor

What does this PR do?

Adds API tests for caching, making it relatively trivial to add new cache backends that conform to the default/localfs cache behavior. It also fixes a few inconsistencies (in particular the missing updated functionality for a few of the backends.)

This also functions as regression tests since it will ensure that any changes to cache backends that alter existing cache behavior will be exposed.

This will also be super useful when we upgrade mysql/redis/consul/etcd libraries and databases.

What issues does this PR fix or reference?

Closes #61081
Closes #60273
Closes #60272

Previous Behavior

Cache backends were somewhat inconsistent. Also almost completely untested.

New Behavior

All current cache backends behave identically and completely like the localfs cache backend

Merge requirements satisfied?

[NOTICE] Bug fixes or features added to Salt require tests.

Commits signed with GPG?

Yes

Please review Salt's Contributing Guide for best practices.

See GitHub's page on GPG signing for more information about signing commits with GPG.

waynew added 16 commits November 2, 2021 14:13
There was a bug in some of the lookups where it was looking at sub banks
instead of the banks containing the leaf/key nodes. That was fixed. Also
restored the original behaviors, only adding the updated functionality,
namely adding timestamp key/values to redis.
Previously on X-Files. Wait nope. Previously on etcd cache files...
:joy: if a path were 1/2/3 then etcd would break with IndexError, but
when it was 1/2/3/4/5 then instead of returning the "file" (key?) 5 it
would return `4/5` as the name. Which is probably not correct, and
definitely inconsistent with localfs (and every other cache module).

Now it will always only return the final segment of the cache (e.g. 7
for 1/2/3/4/5/6/7 and 5 for 1/2/5)
Comments were outdated, now they're gone or fixed. Also shifted around
some of the test setup to make it a bit cleaner.
Fixed an inconsistency when flushing banks with None key. Also added
to the test run. We may want to add other backing caches via alternate
fixtures?
Added a force_reconnect function because I couldn't figure out how to
remove the client from the context any other way. There may be a better
way to do that, but there will probably be some loader changes anyway.
@waynew
waynew requested a review from a team as a code owner November 11, 2021 18:42
@waynew
waynew requested review from dwoz, garethgreenaway, krionbsd and s0undt3ch and removed request for a team November 11, 2021 18:42
Comment thread salt/cache/etcd_cache.py Resolved
Saltfile never should have been added in the first place and the
integration test wasn't really necessary, functional was better/lighter.
Comment thread salt/cache/mysql_cache.py Resolved
Comment thread salt/cache/mysql_cache.py
Comment on lines +354 to +355
data = (bank, key)
cur, _ = run_query(__context__["mysql_client"], query=query, args=data)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I could probably unify the names here - that is make data -> args everywhere.

Comment thread salt/loader/lazy.py Resolved
Comment on lines +130 to +150
with container.started() as factory:
# TODO: May want to do the same thing for redis to ensure that service is up & running
# TODO: THIS IS HORRIBLE. THERE ARE BETTER WAYS TO DETECT SERVICE IS UP -W. Werner, 2021-10-12

timer = Timer(timeout=10)
sleeptime = 0.1
while not timer.expired:
try:
with socket.create_connection(
("localhost", consul_port), timeout=1
) as cli:
cli.send(b"GET /v1/kv/fnord HTTP/1.1\n\n")
cli.recv(2048)
break
except ConnectionResetError as e:
if e.errno == 104:
time.sleep(sleeptime)
sleeptime += sleeptime
else:
assert False, "Timer expired before connecting to consul"
yield factory

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@s0undt3ch do you know of a better way to wait/timeout for when a container service comes online? I learned that the container will come up but the process inside won't actually be ready for varying times. I hacked this together because I wasn't aware of a better way to do it... do you have any examples?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I don't think so - the ports can be open but the service isn't actually ready. This seems like the best option then, currently.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Maybe we should investigating supporting making requests against services and assert on the responses if/when the service is up....

Comment on lines +377 to +379
pytest.skip(
"This is impossible with redis. Should we make localfs behave the same way?"
)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This might be a question for the team - redis can't have an empty set, which is how the cache backend implements "folders". Localfs doesn't actually remove the bank (folder) when there are no files in the directory. Either we can update localfs, or just continue skipping this for now.

Comment on lines +411 to +413
pytest.skip(
"Skipping for now - etcd, redis, and mysql do not raise. Should ensure all backends behave consistently"
)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is another accidental API. It's only the localfs cache that raises but I'm pretty sure it should not.

Fixes saltstack#60272 - the import statement was totally incorrect. Now it will
actually attempt to connect to a redis cluster.
From what I can tell, previously the query was being built in Python
instead of using mysql to add the parameters, and this probably horked
up the handling of binary strings.

Unless some other version of the DB will be problematic...

@s0undt3ch s0undt3ch left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

As for the consistency questions, we should strive to be consistent across backends...

Comment thread salt/cache/__init__.py Resolved
Comment thread salt/cache/consul.py Resolved
Comment thread salt/cache/mysql_cache.py Resolved
Comment thread salt/cache/redis_cache.py Resolved
Comment thread salt/cache/redis_cache.py Resolved
Comment on lines +130 to +150
with container.started() as factory:
# TODO: May want to do the same thing for redis to ensure that service is up & running
# TODO: THIS IS HORRIBLE. THERE ARE BETTER WAYS TO DETECT SERVICE IS UP -W. Werner, 2021-10-12

timer = Timer(timeout=10)
sleeptime = 0.1
while not timer.expired:
try:
with socket.create_connection(
("localhost", consul_port), timeout=1
) as cli:
cli.send(b"GET /v1/kv/fnord HTTP/1.1\n\n")
cli.recv(2048)
break
except ConnectionResetError as e:
if e.errno == 104:
time.sleep(sleeptime)
sleeptime += sleeptime
else:
assert False, "Timer expired before connecting to consul"
yield factory

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Maybe we should investigating supporting making requests against services and assert on the responses if/when the service is up....

@Ch3LL Ch3LL added the Phosphorus v3005.0 Release code name and version label Feb 16, 2022

@Ch3LL Ch3LL left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

just one question

s0undt3ch
s0undt3ch previously approved these changes Mar 2, 2022
Ch3LL
Ch3LL previously approved these changes Mar 3, 2022
I really really hate the way this is configured but I can't make pytest
use fixtures as parameters in fixtures. I may have to struggle to come
up with another way.
@waynew
waynew dismissed stale reviews from Ch3LL and s0undt3ch via 213b124 March 18, 2022 22:43
@Ch3LL

Ch3LL commented Apr 6, 2022

Copy link
Copy Markdown
Contributor

lint is failing

waynew added 4 commits April 6, 2022 16:28
This has been tested locally and it runs all of them. I'm sure with the
CI environment it's possible that it fails for no particularly good
reason. And it's definitely not worth blocking a PR for (though arguably
it could be if *all* of the images fail, but...)
@waynew

waynew commented Apr 7, 2022

Copy link
Copy Markdown
Contributor Author

Lint should be good now, it was failing on one test for flakiness - just pushed a fix for that. The other one the whole centos 8 stream failed to even launch 🤷

We'll see if they all pass now...

@github-actions

github-actions Bot commented Apr 7, 2022

Copy link
Copy Markdown
Contributor

Hi! I'm your friendly PR bot!

You might be wondering what I'm doing commenting here on your PR.

Yes, as a matter of fact, I am...

I'm just here to help us improve the documentation. I can't respond to
questions or anything, but what I can do, I do well!

Okay... so what do you do?

I detect modules that are missing docstrings or "CLI Example" on existing docstrings!
When I was created we had a lot of these. The documentation for these
modules need some love and attention to make Salt better for our users.

So what does that have to do with my PR?

I noticed that in this PR there are some files changed that have some of these
issues. So I'm leaving this comment to let you know your options.

Okay, what are they?

Well, my favorite, is that since you were making changes here I'm hoping that
you would be the most familiar with this module and be able to add some other
examples or fix any of the reported issues.

If I can, then what?

Well, you can either add them to this PR or add them to another PR. Either way is fine!

Well... what if I can't, or don't want to?

That's also fine! We appreciate all contributions to the Salt Project. If you
can't add those other examples, either because you're too busy, or unfamiliar,
or you just aren't interested, we still appreciate the contributions that
you've made already.

Whatever approach you decide to take, just drop a comment here letting us know!

Detected Issues (click me)
Check Known Missing Docstrings...........................................Failed
- hook id: invoke
- duration: 1.05s
- exit code: 1

The function 'init_kwargs' on 'salt/cache/redis_cache.py' does not have a docstring
Found 1 errors


Thanks again!

@waynew waynew self-assigned this Apr 7, 2022
@Ch3LL
Ch3LL merged commit deeeaa3 into saltstack:master Apr 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Phosphorus v3005.0 Release code name and version

Projects

None yet

4 participants